Wednesday 15 February 2012

javascript - Variable content outside of a method -



javascript - Variable content outside of a method -

i'm working on getting jsonp info page , want run various functions on information. info comes fine can't seem find way create accessible outside of function. know it's closure , function scope can't figure out how create work, ideas?

i can accomplish i'm trying in rest of script making multiple calls json file assume it's improve query json 1 time , pop variable , seek work off that? i'm relatively new set suggestions appreciated.

effectively code below want able allmatches variable accessible outside of getdata method after runs.

thanks time, help appreciated.

var appinfo = { getdata : function(){ var responsejsonvar; var callbackname, script, newinfo, mydata,allmatches; // random name callback callbackname = "checkgames" + new date().gettime() + math.floor(math.random() * 10000); // create jsonp script phone call on page script = document.createelement('script'); script.src = "http://www.hookhockey.com/index.php/temp-gillian/?callback=" + callbackname; document.documentelement.appendchild(script); // phone call json window[callbackname] = function(data) { responsejsonvar = data; // info json file //the filtered info source json var allmatches = responsejsonvar["matches"]; console.dir('allmatches within function: ' + allmatches); //this comes fine // remove our callback ('delete' 'window properties fails on versions of ie, fall setting property 'undefined' if happens) seek { delete window[callbackname]; } grab (e) { window[callbackname] = undefined; } //i've tried putting homecoming value (return allmatches) in here , calling window[callbackname]() outside of function undefined matches }; // end window[callbackname] function //this think should doing info out on own console.dir('allmatches outside function: ' + allmatches); //this doesn't come 'allmatches not defined' } //end getdata method } //end appinfo appinfo.getdata();

you create property on appinfo object called allmatches , set property when info comes jsonp call:

var appinfo = { allmatches: null, // new property hold returned info confirmdataavailableoutsidefunction: function () { // new function verify info available outside getdata() console.dir('appinfo.allmatches outside function after jsonp phone call executes: ' + appinfo.allmatches); //this doesn't come 'allmatches not defined' }, getdata: function () { var responsejsonvar; var callbackname, script, newinfo, mydata, allmatches; // random name callback callbackname = "checkgames" + new date().gettime() + math.floor(math.random() * 10000); // create jsonp script phone call on page script = document.createelement('script'); script.src = "http://www.hookhockey.com/index.php/temp-gillian/?callback=" + callbackname; document.documentelement.appendchild(script); // phone call json window[callbackname] = function (data) { responsejsonvar = data; // info json file //the filtered info source json appinfo.allmatches = responsejsonvar["matches"]; // store info in allmatches property console.dir('allmatches within function: ' + appinfo.allmatches); //this comes fine appinfo.confirmdataavailableoutsidefunction(); // phone call test method verify allmatches property set // remove our callback ('delete' 'window properties fails on versions of ie, fall setting property 'undefined' if happens) seek { delete window[callbackname]; } grab (e) { window[callbackname] = undefined; } //i've tried putting homecoming value (return allmatches) in here , calling window[callbackname]() outside of function undefined matches }; // end window[callbackname] function //this think should doing info out on own console.dir('appinfo.allmatches outside function before jsonp phone call executes: ' + appinfo.allmatches); //this doesn't come 'allmatches not defined' } //end getdata method }; //end appinfo appinfo.getdata();

note modified text of sec console.dir indicate it's running before jsonp phone call returns, , hence allmatches property still null @ point.

that's why, after implementing @peter-b's suggestion utilize window.allmatches instead of local variable allmatches, window.allmatches outside function undefined--you checking before set.

@peter-b's solution work fine, provided didn't seek access window.allmatches before set. if want info stored in global variable, can utilize method; if you'd rather have stored on appinfo object, can utilize mine.

alternatively, can wrap in immediate function has allmatches local variable:

(function () { var allmatches = null; var appinfo = { getdata: function (datareadycallback) { /* ... */ allmatches = responsejsonvar["matches"]; datareadycallback(); /* ... */ } }; appinfo.getdata(allmatchesready); function allmatchesready() { console.dir('allmatches outside function after jsonp phone call executes: ' + allmatches); } }());

javascript json jsonp

No comments:

Post a Comment