Question

In a Sencha touch aplication, can I use some sort of ajax prefilter and supply a URL root for all my requests (from stores and models)?

One more thing,

Is there a way to add a AUTH-TOKEN header, alto to every ajax request?

Thank you!

Was it helpful?

Solution

Ext.Ajax.request is a singleton instance. You can tap on beforerequest event and change/modify url, add/remove headers and do all other modification as needed. This way you can capture all your ajax request. You should be able to do this in your app.launch function.

 Ext.Ajax.on('beforerequest', function(conn, request, eOpts){
      // Your implementaion here.  change request.url, request.headers etc
 });

OTHER TIPS

If you're setting the URL on your store proxies via Ext.data.proxy.Ajax.url, you could do this in one of two ways.

One: you can extend (or override) the class, adding the logic yourself.

Two: you could configure your URLs as such:

//someplace define this...
var urlPrefix = function() {
    return '/path/to/API/';
};

//in your store/proxy
Ext.define('MyApp.store.Foo', {
    config : {
        proxy : {
            type : 'ajax',
            url  : urlPrefix() + 'API_endpoint'
        }
    }
});

I've done that a bunch of times before.

As for your Auth headers, you can override Ext.data.Connection to include additional headers. The key here is that you would need to override it very early in the application lifetime, because other classes have unique instances of Ext.data.Connection (for example, Ext.Ajax).

But before giving a recommendation on how to apply the headers globally, I'd need to know more about what you needed to do. There's many things to consider before doing that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top