PDA

View Full Version : How to pass a variable to extraParams of store



ragecf
17 Jul 2013, 12:15 AM
Hi everybody,

I want to pass a variable to extraParams of store. But It doesn't work.



Ext.define('myModel', {
extend: 'Ext.data.Model',

fields: [
{name: 'name', type: 'auto'},
{name: 'value', type: 'auto'}
]
});






Ext.define('myStore, {
extend: 'Ext.data.Store',

model: 'myModel,
autoLoad: false,
proxy: {
type: 'ajax',
url: 'xxx.php',
reader: {
type: 'json',
root: 'root'
},
sortParam: null,
pageParam: null,
startParam: null,
limitParam: null,
extraParams: {
param: this.myParam
}
}
});






Ext.define('myGrid', {extend: 'Ext.grid.Panel',
initComponent: function() {
this.store = Ext.create('myStore', {
myParam: 'something according to different condition'
});

this.columns = [......];

Ext.apply(this, {
title: 'My Grid'
});
this.callParent(arguments);
});
}
});





var grid = Ext.create('myGrid');
grid.renderTo(document.body);

fily55
17 Jul 2013, 2:44 AM
I'm not sure because I never create stores this way but should it not be:



Ext.define('myGrid', {
extend: 'Ext.grid.Panel',
initComponent: function() {
this.store = Ext.create('myStore', {
proxy : {
extraParams : {
myParam: 'something according to different condition'
}
}
});
this.columns = [......];
Ext.apply(this, { title: 'My Grid' });
this.callParent(arguments); }); } });

Just a guess...

julio.batista
17 Jul 2013, 3:08 AM
extraParams: {
myParam: this.myParam
}

It's a static code, so the this probably is referencing the window object and it's just evaluated when the class is instantiated, not when it's created.

You should do something like


Ext.define('mystore', {
extend: 'Ext.data.Store',
constructor: function()
{
this.extraParams.myParam = this.myParam;
this.callParent(arguments);
}
});

ragecf
17 Jul 2013, 10:08 PM
It's do strange to create store like this way. We offen use Ext.create to create store in grid directly instead of Ext.define. But we should define store independant of grid in MVC model.

So I want to know how to ...

ragecf
17 Jul 2013, 10:12 PM
This code is OK now.




Ext.define('myStore', {
extend: 'Ext.data.Store',
constructor: function(config) {
this.autoLoad = true;
this.model = 'myModel';
this.proxy = {
type: 'ajax',
url: 'xxx.php',
reader: {
type: 'json',
root: 'root'
},
extraParams: {
myParam: config.myParam
}
};
this.callParent(arguments);
})


But why firebug show me.model is undefined if I used initComponent instead of constructor?

julio.batista
18 Jul 2013, 3:04 AM
Because the store gets the model in the constructor. You can still have the separation, what you need to do is just put your own configs in your store class.


Ext.define('myGeneicStore', {
// ...
myParam: '',
constructor: function()
{
this.extraParams.myParam = this.myParam;
this.callParent(arguments);
}
});

// in code
Ext.create('myGenericStore', {
// ...
model: 'myModel',
myParam: 'something'

});