PDA

View Full Version : setting proxy extraParams set it for all new instances ?



dedoz
21 Jun 2012, 11:45 PM
hi , with this code



this.store1 = Ext.create('MUI.store.persona');
this.store1.getProxy().extraParams = {scope : 'minimal'};
this.store1.load();

this.store2 = Ext.create('MUI.store.persona');
this.store2.load(); // sends scope:minimal


so setting the extraParams for the first instance (store1), will set that extraParam for all new instances ?
thats sad D:

also loading 1 record using model load method, will have the same extraParams



MUI.store.persona.load(1); /// sends extraParams other stores/model instances setted


so all instances of a model share the same instance of the proxy ?
in the code u dont know if someone else (instance) setted params or not, and if u set the extraParam = {} to be sure nothing else is sent, then u delete other instances params D:

each store cant have its own extraParams ? D: its not the same as doing store.load({params:...}) because this way those params are lost.

or im doing something wrong ? should i set extraParams for each instance in another way ? should i declare a new proxy config for each time i create a store instance of the same model ? wich is ugly :d

mitchellsimoens
6 Jul 2012, 2:01 PM
How is your proxy defined?

wemerson.januario
30 Dec 2013, 5:19 PM
I am having this same problem. Extra Params are shared with all new stores instances. Is it a bug? Am I doing something wrong while instatiating stores?

mitchellsimoens
31 Dec 2013, 5:57 AM
This is likely because your object is being shared. You need to create a new object for each proxy instance. How are you defining the extraParams?

wemerson.januario
31 Dec 2013, 6:30 AM
This is likely because your object is being shared. You need to create a new object for each proxy instance. How are you defining the extraParams?

I am using proxy on Model

And setting params via store.getProxy().setExtraParams({key: value});

If I configure proxy in stores it works. but i really need to use in models. i know that model is a singleton intance and obviously will share configs. I need a workaround or better pratic to keep using proxy on models and not share models instances with same stores

This is my model


Ext.define('App.model.administracao.PermissaoModel', {
extend: 'Ext.data.Model',
idProperty: 'id',
singleton: false,
proxy: Ext.create('Ext.data.proxy.Rest', {
url: 'permission',
api: {
read: 'permission/read',
create: 'permission/create',
update: 'permission/update',
destroy: 'permission/destroy'
},
reader: {
type: 'json',
idProperty: 'id',
messageProperty: 'message',
root: 'data'
},
writer: {
type: 'json',
writeAllFields: false,
encode: true,
root: 'data'
}


}),
fields: [
{
name: 'id',
type: 'int',
useNull: true
},
{
name: 'name'
},
{
name: 'display_name'
},
{
name: 'recurso',
type: 'int',
useNull: true
},
{
name: 'created_at',
type: 'date',
dateFormat: 'Y-m-d H:i:s',
persist: false
},
{
name: 'updated_at',
type: 'date',
dateFormat: 'Y-m-d H:i:s',
persist: false
},
{
name: 'deleted_at',
type: 'date',
dateFormat: 'Y-m-d H:i:s',
persist: false
}
],
validations: [
{
type: 'presence',
field: 'name'
},
{
type: 'presence',
field: 'display_name'
}
]
});


This is my store




Ext.define('App.store.administracao.PermissaoStore', {
extend: 'Ext.data.Store',
requires: [
'App.model.administracao.PermissaoModel'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
model: 'App.model.administracao.PermissaoModel'
}, cfg)]);
}
});

Thanks

mitchellsimoens
31 Dec 2013, 7:08 AM
You are creating a single instance and setting it to a property on the model's prototype so that proxy will be shared globally. You need to use config objects instead of instances.

wemerson.januario
31 Dec 2013, 7:42 AM
Could you post an usage example .How use config object?

Thanks for your support!

mitchellsimoens
31 Dec 2013, 7:52 AM
proxy: {
type: 'rest',
url: 'permission',
api: {
read: 'permission/read',
create: 'permission/create',
update: 'permission/update',
destroy: 'permission/destroy'
},
reader: {
type: 'json',
idProperty: 'id',
messageProperty: 'message',
root: 'data'
},
writer: {
type: 'json',
writeAllFields: false,
encode: true,
root: 'data'
}
}

wemerson.januario
31 Dec 2013, 8:03 AM
ohh yes. Now I can understand what you mean. but Amazingly, my proxy was setted like this having the same issue.this is my new model code





Ext.define('App.model.administracao.PermissaoModel', {
extend: 'Ext.data.Model',
idProperty: 'id',
proxy: {
type: 'rest',
url: 'permission',
api: {
read: 'permission/read',
create: 'permission/create',
update: 'permission/update',
destroy: 'permission/destroy'
},
reader: {
type: 'json',
idProperty: 'id',
messageProperty: 'message',
root: 'data'
},
writer: {
type: 'json',
writeAllFields: false,
encode: true,
root: 'data'
}
},
fields: [
{
name: 'id',
type: 'int',
useNull: true
},
{
name: 'name'
},
{
name: 'display_name'
},
{
name: 'recurso',
type: 'int',
useNull: true
},
{
name: 'created_at',
type: 'date',
dateFormat: 'Y-m-d H:i:s',
persist: false
},
{
name: 'updated_at',
type: 'date',
dateFormat: 'Y-m-d H:i:s',
persist: false
},
{
name: 'deleted_at',
type: 'date',
dateFormat: 'Y-m-d H:i:s',
persist: false
}
],
validations: [
{
type: 'presence',
field: 'name'
},
{
type: 'presence',
field: 'display_name'
}
]
});

mitchellsimoens
31 Dec 2013, 8:04 AM
Put the proxy on the store. The model definition is a single instance but the store will create a new proxy instance with each store instance.

wemerson.januario
31 Dec 2013, 8:04 AM
I Think this problem is because singleton model instance while define store and pass string model name instead create a new

wemerson.januario
31 Dec 2013, 8:08 AM
Put the proxy on the store. The model definition is a single instance but the store will create a new proxy instance with each store instance.

yes if try putting proxy on store. It works perfectly, but I really need use proxy on models, cause i use some model methods to persist directly on models. like. model.save(.....);

is there any chance to have multiples models instances on store?

wemerson.januario
31 Dec 2013, 9:07 AM
I have solved my problem with this.

cloning model proxy instead use model proxy


Now I can keep using proxy on models and will be sure that each store instance will have its own proxy clone or definition.

thanks http://www.sencha.com/forum/showthread.php?211652
(http://www.sencha.com/forum/showthread.php?211652)


Ext.define('App.store.administracao.PermissaoStore', {
extend: 'Ext.data.Store',
requires: [
'App.model.administracao.PermissaoModel'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
model: 'App.model.administracao.PermissaoModel'
}, cfg)]);
//clone proxy
var proxy = Ext.Object.merge({}, me.getProxy());
me.setProxy(proxy);
}
});

mitchellsimoens
31 Dec 2013, 9:15 AM
That's just a waste of resources, at the least you should use the initialConfig of the proxy instead of the instance.