从异步错误转移到异步真 [英] moving from async false to async true
问题描述
所以我有一张code运行了好几次
Hi so I have a piece of code that run quite a few times
Loader.load(数据/ map.json')
Loader.load('data/map.json')
的想法是,我可以传递任何JSON文件加载程序将处理它
the idea is I can pass in any json file and the loader will deal with it
所以这里是异步错误格式的加载器
so here is the loader in async false format
var Loader = {
basePath: '',
cache: {},
load: function(name) {
if(typeof this.cache[name] == 'undefined') {
var loadUrl = (this.basePath == '') ? name : this.basePath + '/' + name;
var parameters = {
url: loadUrl,
dataType: 'json',
async: false,
context: this,
success: function(data) {
this.cache[name] = data;
}
};
$.ajax(parameters);
}
return this.cache[name];
}
};
return Loader;
但这是异步假的格式,我需要得到这个异步真正的格式与jQuerymobile更好的工作,所以对这里的一些帮助,我设法得到它的一个点,它的异步
but this is in async false format and I need to get this to async true format to work better with jQuerymobile so with some help on here I managed to get it to a point where its async
var data = AsyncLoader.load('data/map.json').done(function(data, textStatus, jqXHR) {
console.log("complete");
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log("an error has occurred");
}).always(function(jqXHR, textStatus) {
console.log("running");
});
此调用的asynloader
this call the asynloader
function($) {
var AsyncLoader = {
basePath: '',
cache: {},
load: function(name) {
if(typeof this.cache[name] == 'undefined') {
var loadUrl = (this.basePath == '') ? name : this.basePath + '/' + name;
var parameters = {
beforeSend: function() { $.mobile.showPageLoadingMsg(); },
complete: function() {$.mobile.hidePageLoadingMsg(); },
url: loadUrl,
dataType: 'json',
async: true,
context: this,
success: function(data) {
this.cache[name] = data;
}
};
$.ajax(parameters);
}
return this.cache[name];
}
};
return AsyncLoader;
问题是使这项工作我需要返回$阿贾克斯(参数),而不是返回this.cache [名]否则,我得到一个JavaScript错误......问题是,如果我改变它获得$阿贾克斯(参数),当我在maps.json加载第二次它不会从this.cache它会加载数据JSON文件又是没用的。
the problem is to make this work I need to return $.ajax(parameters) rather than return this.cache[name]; otherwise I get a javascript error.. the problem is if I change it to get $.ajax(parameters) when I load in maps.json for a second time it doesn't get the data from the this.cache it will load the json file again which is useless
谁能帮助
感谢:)
推荐答案
$。阿贾克斯
返回的一个延迟
对象。你也可以回到自己的延迟
对象。你的新的code可能看起来有点像这样的:
$.ajax
returns a Deferred
object. You, too, can return your own Deferred
object. Your new code might look a little like this:
load: function(name) {
// Is the object already in the cache?
if(Object.prototype.hasOwnProperty.call(this.cache, name)) {
// Yes! Return a Deferred that's already been resolved.
return jQuery.Deferred().resolve(this.cache[name]);
}else{
// No! We have to load it. Let's still use our own Deferred, though:
var deferred = jQuery.Deferred();
// There's our Deferred. Now let's load it.
jQuery.ajax({
// ...
}).done(function(data) {
// We loaded it successfully!
// Add it to our cache and resolve the Deferred.
this.cache[name] = data;
deferred.resolve(data);
}).fail(function() {
// Fail! Pass the failure on to our deferred.
deferred.reject.apply(deferred, arguments);
});
// We've started the AJAX request. Now return our Deferred.
return deferred;
}
}
这篇关于从异步错误转移到异步真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!