AlexC
AlexC

Reputation: 9661

Sencha Touch 2, Ajax Promise

I am looking for the Best practice in one use case, (using sencha touch 2 framework) :

I have multiple ajax calls (JSONP response), and I want to filter feed data by date between all of those response. The problem is I do not when to start filtering. I want to know when all ajax is completed.

in jQuery I would use, "promises", for ex:

    $.when($.ajax("/page1.php"), $.ajax("/page2.php"), ... $.ajax("/pageN.php")).done(function(a1,  a2){
      // do something
    });

, I cannot find any similar "Deferred" in sencha touch 2, or maybe you can suggest me another way to detect and data was loaded.

I appreciate any suggestion help or the best practice in this situation.

UPDATES:

Yes, I use everywhere the sencha touch "Ext.Ajax.request".

I tried to use something like this , to catch all requests:

    Ext.Ajax.on('beforerequest', this.dosomething, this);
    Ext.Ajax.on('requestcomplete', this.dosomething, this);
    Ext.Ajax.on('requestexception', this.dosomething, this);

But nothing gets fired, in any of these 3 cases,

PS: I tried to execute this event on my main mage is "initialized" as well when i "lunch" the whole application, in any situation it doesn't fire anything.

Is this something wrong when and how I try to fire the event.

Thanks in advice !!!

Upvotes: 2

Views: 963

Answers (2)

Patrick Neeteson
Patrick Neeteson

Reputation: 41

It's not perfect, but this never fails for me:

    function removeMaskIfNoAjaxCalls() {
        //clean up mask with timer, because in the Ext.Ajax.request there is one request (current) 
        setTimeout(function() {
            if (Object.keys(Ext.Ajax.requests).length == 0) Ext.Viewport.setMasked(false);
        }, 2000)
    }

Upvotes: 1

OhmzTech
OhmzTech

Reputation: 897

Assuming you are using Ext.Ajax.request, and want to make all requests together at the same time, there are a few ways to do this. When each request is made you can increment a simple counter, and decrement that counter in the request callback functions. In the callback functions also check if the counter is back to zero - if so then all requests have been completed, and you can now combine all your data. Relatively simple.

Another option is to utilize Ext.Ajax.requests object - which contains all the current requests that have not been completed. As long as there aren't other requests starting between when these ones start/finish you can just check this object in your callback function, and if there are no requests then you can handle your data.

See this link for documentation on Ext.Ajax.

Upvotes: 1

Related Questions