J. Doe
J. Doe

Reputation: 19

Ext JS 4.2: Add "Source Call" parameter to all AJAX calls

I have three different areas in my application that can modify hours. They all use the same grid, store, and model (but different instances) and call the same backend controller. I am trying to implement a way to add a parameter to the AJAX calls, so the backend knows which area of the application that the call came from through the use of a parameter to AJAX calls.

I have attempted the following:

(1) Overriding request function in Ext.data.Connection

Ext.data.Connection.override({
   request: function(options){
      var me = this;
      if(!options.params)
        options.params = {};
        options.params.location = 'location 1';
      return me.callOverridden(arguments);
}});

Result: I couldn't figure out a way to find the module that made the call.

(2) Adding the following to the controllers init of the controllers

Ext.Ajax.on('beforerequest', function(conn, options) {    
   if(!options.params)
     options.params = {};
   options.params.location = "location 1";
});

Result: Every call was sending the same location even if it was a different area

Upvotes: 1

Views: 277

Answers (1)

Alexander
Alexander

Reputation: 20224

Considering that you are using the same store but different instances, the easiest way is to use the store methods of synchronization only. You would have to define the proxy on the store, not the model, then you can easily add a special extraParam on every instance:

Ext.define('MyStore', {
    extend: 'Ext.data.Store',
    proxy: {
        type: 'ajax'
        url: 'test.json'
    }
});

var instance1 = Ext.create('MyStore');
var instance2 = Ext.create('MyStore');
var instance3 = Ext.create('MyStore');
instance1.getProxy().setExtraParam('source', 'instance1');
instance2.getProxy().setExtraParam('source', 'instance2');
instance3.getProxy().setExtraParam('source', 'instance3');
instance1.load();
instance2.load();
instance3.load();

and to sync:

instance1.getAt(0).set('text', 'testinstance1');
instance2.getAt(0).set('text', 'testinstance2');
instance3.getAt(0).set('text', 'testinstance3');
instance1.sync();
instance2.sync();
instance3.sync();

See in action here: https://fiddle.sencha.com/#view/editor&fiddle/2fl4

Open the network tab to see the parameter:

Upvotes: 1

Related Questions