[英]Callback to a class instantiating another class making AJAX request
This is a somewhat difficult question to word. 这是一个很难回答的问题。 Here is the basic process:
这是基本过程:
Once an ajax request has completed, I want to call a method that is on the class in step #1. 一个ajax请求完成后,我想在步骤#1中调用该类上的方法。 What is a good way to achieve this?
什么是实现此目标的好方法?
Here is a jsFiddle of what I'm trying to do: http://jsfiddle.net/twiz/3PRma/4/ 这是我要执行的操作的jsFiddle: http : //jsfiddle.net/twiz/3PRma/4/
The same code is below: 相同的代码如下:
//The global object that makes an ajax call
///////////////////////////////////////////////
globallyDoStuff={
somethingWithACallback: function(url,callback){
$.get(url,{},function(){
// WHAT do I do here to call the
// "doSomethingToAllTheClasses" method?
});
}
}
// A class used to hold an array of classes
///////////////////////////////////////////////
var SomeClassCollection = function(arrayOfURLs){
this.arrayOfClasses=[];
for(var i=0;i<arrayOfURLs.length;i++){
this.addSomeClass(arrayOfURLs[i]);
}
};
SomeClassCollection.prototype={
addSomeClass: function(theUrl){
this.arrayOfClasses.push(new SomeClass(theUrl));
},
doSomethingToAllTheClasses: function(){
// I WANT TO CALL THIS EACH TIME AN AJAX REQUEST IS COMPLETED
console.log(this.arrayOfClasses);
}
}
//The class that calls the global ajax object's method
///////////////////////////////////////////////
var SomeClass = function(theUrl){
this.globalAction(theUrl);
};
SomeClass.prototype={
globalAction: function(theUrl){
globallyDoStuff.somethingWithACallback(theUrl);
}
}
//List of urls
///////////////////////////////////////////////
var urls=[
"/echo/json/",
"/echo/json/",
"/echo/json/",
"/echo/json/",
"/echo/json/",
]
//Create the instance
///////////////////////////////////////////////
var someInstance = new SomeClassCollection(urls);
It seems to me this is a broader problem with your architecture, however this is doable. 在我看来,这是您的体系结构更广泛的问题,但这是可行的。
$.get
returns an XHR object, you can use the return value and hook on its 'success'. $.get
返回一个XHR对象,您可以使用返回值并挂钩其“成功”。
You can change globalAction
to 您可以将
globalAction
更改为
globalAction: function(theUrl){
return globallyDoStuff.somethingWithACallback(theUrl);
}
Then change the SomeClass
constructor to 然后将
SomeClass
构造函数更改为
var SomeClass = function(theUrl){
var result = this.globalAction(theUrl);
//note, you now fill the object here, in the returned part
//when a constructor returns an object it behaves like a normal function
return {callRes:result,...};
};
Then change addSomeClass to 然后将addSomeClass更改为
addSomeClass: function(theUrl){
var addedClass = new SomeClass(theUrl);
this.arrayOfClasses.push(addedClass);
addedClass.callRes.done(function(){
//your code executes here! EACH TIME AN AJAX REQUEST IS COMPLETED
}
},
Note, you can also hook on the jQuery
global ajaxComplete
method: 注意,您还可以挂钩
jQuery
全局ajaxComplete
方法:
$.ajaxComplete(function(){
//this executes whenever ANY AJAX request is complete!
}
You can add an if
check to it, see the API 您可以向其中添加
if
检查,请参阅API
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.