我正在努力从页面获取jsonp信息,我想在该信息上运行各种功能。信息很好,但我似乎无法找到一种方法使其在函数之外可访问。我知道它与闭包和功能范围有关,但我无法弄清楚如何使其工作,任何想法?
我可以通过多次调用json文件来实现我在脚本的其余部分中尝试做的事情,但我认为最好只查询json一次并将其弹出到变量中并尝试解决这个问题?我对这个设置比较新,所以任何建议都值得赞赏。
从下面的代码中,我希望能够在运行后获取getData方法之外的allMatches变量。
感谢您的时间,所有人都非常感谢。
var AppInfo = {
getData : function(){
var responseJsonVar;
var callbackName, script, newInfo, mydata,allMatches;
// Get a random name for the callback
callbackName = "checkGames" + new Date().getTime() + Math.floor(Math.random() * 10000);
// create the jsonP script call on the page
script = document.createElement('script');
script.src = "http://www.hookhockey.com/index.php/temp-gillian/?callback=" + callbackName;
document.documentElement.appendChild(script);
// call the json
window[callbackName] = function(data) {
responseJsonVar = data; // this is the info back from the json file
//the filtered data source from json
var allMatches = responseJsonVar["matches"];
console.dir('allMatches inside the function: ' + allMatches); //this comes back fine
// Remove our callback ('delete' with 'window properties fails on some versions of IE, so we fall back to setting the property to 'undefined' if that happens)
try {
delete window[callbackName];
}
catch (e) {
window[callbackName] = undefined;
}
//I've tried putting a return value (return allMatches) in here and then calling window[callbackName]() outside of the function but I get undefined for matches
}; // end window[callbackName] function
//this is what I think I should be doing to get the info out on its own
console.dir('allMatches OUTSIDE the function: ' + allMatches); //this doesn't come back 'allMatches is not defined'
} //end getdata method
} //end AppInfo
AppInfo.getData();
答案 0 :(得分:1)
您可以在名为AppInfo
的{{1}}对象上创建一个属性,并在数据从jsonp调用返回时设置该属性:
allMatches
请注意,我修改了第二个var AppInfo = {
allMatches: null, // NEW PROPERTY TO HOLD RETURNED DATA
confirmDataAvailableOutsideFunction: function () { // NEW FUNCTION TO VERIFY DATA AVAILABLE OUTSIDE getData()
console.dir('AppInfo.allMatches OUTSIDE the function AFTER jsonp call executes: ' + AppInfo.allMatches); //this doesn't come back 'allMatches is not defined'
},
getData: function () {
var responseJsonVar;
var callbackName, script, newInfo, mydata, allMatches;
// Get a random name for the callback
callbackName = "checkGames" + new Date().getTime() + Math.floor(Math.random() * 10000);
// create the jsonP script call on the page
script = document.createElement('script');
script.src = "http://www.hookhockey.com/index.php/temp-gillian/?callback=" + callbackName;
document.documentElement.appendChild(script);
// call the json
window[callbackName] = function (data) {
responseJsonVar = data; // this is the info back from the json file
//the filtered data source from json
AppInfo.allMatches = responseJsonVar["matches"]; // store data in allMatches property
console.dir('allMatches inside the function: ' + AppInfo.allMatches); //this comes back fine
AppInfo.confirmDataAvailableOutsideFunction(); // call test method to verify allMatches property is set
// Remove our callback ('delete' with 'window properties fails on some versions of IE, so we fall back to setting the property to 'undefined' if that happens)
try {
delete window[callbackName];
}
catch (e) {
window[callbackName] = undefined;
}
//I've tried putting a return value (return allMatches) in here and then calling window[callbackName]() outside of the function but I get undefined for matches
}; // end window[callbackName] function
//this is what I think I should be doing to get the info out on its own
console.dir('AppInfo.allMatches OUTSIDE the function BEFORE jsonp call executes: ' + AppInfo.allMatches); //this doesn't come back 'allMatches is not defined'
} //end getdata method
}; //end AppInfo
AppInfo.getData();
的文本以指示它在 jsonp调用返回之前正在运行,因此console.dir
属性仍然是{{1}那时候。
这就是为什么在实施@ peter-b的建议后使用allMatches
代替局部变量null
,window.allMatches
OUTSIDE函数是allMatches
- 你正在检查它在它成立之前。
@ peter-b的解决方案可以正常工作,只要您在设置之前没有尝试访问window.allMatches
。因此,如果您希望数据存储在全局变量中,您可以使用他的方法;如果您希望将其存储在AppInfo对象中,则可以使用我的。
或者,您可以将所有内容包装在具有undefined
作为局部变量的立即函数中:
window.allMatches
答案 1 :(得分:0)
获得所需内容的一种简单方法是将allMatches
指定为window
的属性,使其随处可访问。替换
var allMatches = responseJsonVar["matches"];
与
window.allMatches = responseJsonVar["matches"];
然后使用window.allMatches
访问它。