我想访问创建的商店中的参数。下面是我的商店:
Q.define('test.DataStore', Ext.data.ArrayStore, function($supr){
return { constructor: function(cfg) {
$supr().constructor.call(this, {
storeId: 'DataRule',
data :[
['SEARCH', 'Search')],
['SEARCH2', 'Search2')],
['SEARCH3', 'Search3')],
]
});
}
}; }
);
以下是我从以下地址访问商店的方式:
items : this.section({
items : [{
store: 'DataRule' //using the store id defined in the store above }, { xtype : 'box',
width : 10,
}]
}]
我希望将一些参数传递给商店,即[{id:'1'},{id:'2'}]。如何通过params才能访问商店?
谢谢!
答案 0 :(得分:1)
我假设你使用的是ExtJS6,你的目标是通过以下参数过滤结果:
在您的示例中,您可以预先定义数据。考虑到这一点,您应该能够在商店中使用“过滤器”方法:
// use filter method to filter the data on the front-end
var myStore = Ext.create('Ext.data.Store', {
model: 'User',
data : [
{id: '1', firstName: 'Peter', lastName: 'Venkman'},
{id: '2', firstName: 'Egon', lastName: 'Spengler'},
{id: '3', firstName: 'Ray', lastName: 'Stantz'},
{id: '4', firstName: 'Winston', lastName: 'Zeddemore'}
]
});
myStore.filter('id', '1');
// making use of storeId
Ext.getStore('myStore').filter('id', '1');
如果要从服务器请求数据(例如通过ajax请求),使用params请求数据的最简单方法是在加载调用上添加params:
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'},
{name: 'age', type: 'int'},
{name: 'eyeColor', type: 'string'}
]
});
var myStore = Ext.create('Ext.data.Store', {
storeId: 'myStore',
model: 'User',
proxy: {
type: 'ajax',
url: '/users.json',
reader: {
type: 'json',
rootProperty: 'users'
}
},
autoLoad: true
});
// adding params on load
Ext.getStore('myStore').load({
params: {id: '1'}
});
如果要为每个加载事件自动注入参数,可以在'beforeload'事件上创建一个监听器并添加参数:
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'},
{name: 'age', type: 'int'},
{name: 'eyeColor', type: 'string'}
]
});
var myStore = Ext.create('Ext.data.Store', {
storeId: 'myStore',
model: 'User',
proxy: {
type: 'ajax',
url: '/users.json',
reader: {
type: 'json',
rootProperty: 'users'
}
},
autoLoad: true,
listeners: {
// add params on every store load by injecting on the beforeload event
beforeload: function(store, operation, eOpts) {
operation.setParams({id: '1'});
}
}
});
// the beforeload event kicks off our code to inject params
Ext.getStore('myStore').load();