简体   繁体   English

回调到实例化另一个发出AJAX请求的类的类

[英]Callback to a class instantiating another class making AJAX request

This is a somewhat difficult question to word. 这是一个很难回答的问题。 Here is the basic process: 这是基本过程:

  1. A class is instantiated. 实例化一个类。
  2. This class's constructor method then instantiates another class 然后,此类的构造方法实例化另一个类
  3. This new class's constructor method uses a global object's method to make an AJAX request. 这个新类的构造方法使用全局对象的方法发出AJAX请求。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM