PDA

View Full Version : Ext.define instances extraParams being set on all subsequent instances



gfrancis1@gmail.com
27 Aug 2012, 7:38 AM
If I define a Store definition, such as:


Ext.define('ContactStore', { extend: 'Ext.data.Store',
nocache:true,
model : 'CRM_CONTACT',
proxy : {
type : 'ajax',
url : 'index.php?option=com_foo',
reader : {
type : 'json',
root : 'rows'
},
extraParams : {
table : 'CRM_CONTACT_VIEW',
order : 'LASTNAME',
controller : 'Read'
}
},
remoteSort:true,

constructor: function() {
this.callParent(arguments);
}
});

and then instantiate it, and add extra parameters to the *instance's* proxy:



myStore = new ContactStore();
myStore.getProxy().extraParams.RESELLER_ID = 10205;


then *every* instance of ContactStore I create thereafter has the extra parameters set the same way.
Why would this be the case when I am defining extra parameters on the instance and not the definition?
Am I thinking about all this in too much of an Object Oriented way?

scottmartin
27 Aug 2012, 12:55 PM
Are you using MVC pattern? If so .. continue using define and it will be instantiated automatically. If you are not, just create the store.. not both.

You can use just use: extraParams : {}; // clear previous

Scott.

gfrancis1@gmail.com
27 Aug 2012, 1:12 PM
Thanks for your reply. Can you elaborate just a little on the meaning of 'continue using define' .. I guess you are referring to a known MVC methodology for using Ext - using Define for *all* instantiation of JS Objects?
If you could confirm that or point me to a resource it'd really help.
Thanks again,

scottmartin
27 Aug 2012, 1:20 PM
If you could confirm that

Correct ..

When using MVC, it will automatically create the instance as needed.

Scott.

vietits
27 Aug 2012, 6:26 PM
The problem here is by sharing objects on class prototype. In your case, config value for proxy property is an object and this object is shared on prototype of ContactStore. So, all instances from ContactStore will share this object. To fix this, try to move proxy config into class constructor, like below:


Ext.define('ContactStore', {
extend: 'Ext.data.Store',
nocache:true,
model : 'CRM_CONTACT',
// proxy : {
// type : 'ajax',
// url : 'index.php?option=com_foo',
// reader : {
// type : 'json',
// root : 'rows'
// },
// extraParams : {
// table : 'CRM_CONTACT_VIEW',
// order : 'LASTNAME',
// controller : 'Read'
// }
// },
remoteSort:true,

constructor: function() {
var me = this;
me.proxy = {
type : 'ajax',
url : 'index.php?option=com_foo',
reader : {
type : 'json',
root : 'rows'
},
extraParams : {
table : 'CRM_CONTACT_VIEW',
order : 'LASTNAME',
controller : 'Read'
}
};
me.callParent(arguments);
}
});

scottmartin
28 Aug 2012, 5:45 AM
The problem here is by sharing objects on class prototype

Yes sir .. i just plain missed that one .. thanks ;)

Scott.

teaganrichat
21 Nov 2012, 10:02 AM
I think the problem is really with the Ext.data.proxy.Server constructor function. I ran into this issue today myself and the issue is not that the proxies are shared but that the extraParams object is shared across all instances of that proxy. The proxies themselves are fine and can be left in the class definition.

What should change is how Ext.data.proxy.Server sets its extraParams property. Rather than pointing to the extraParams object in the config, it should make a unique copy.

Ext.data.proxy.Server


constructor: function(config) {
var me = this;


config = config || {};
/**
* @event exception
* Fires when the server returns an exception
* @param {Ext.data.proxy.Proxy} this
* @param {Object} response The response from the AJAX request
* @param {Ext.data.Operation} operation The operation that triggered request
*/
me.callParent([config]);


/**
* @cfg {Object} extraParams
* Extra parameters that will be included on every request. Individual requests with params of the same name
* will override these params when they are in conflict.
*/
me.extraParams = config.extraParams || {};


me.api = Ext.apply({}, config.api || me.api);

//backwards compatibility, will be deprecated in 5.0
me.nocache = me.noCache;
}


me.extraParams = config.extraParams || {};
should really be

me.extraParams = config.extraParams ? Ext.clone(config.extraParams) : {};

Tim Eagan
Modus Create