使用新参数重新加载 json 存储 ExtJs Ext.data.JsonStore
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3277682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reloading a json store with new parameters ExtJs Ext.data.JsonStore
提问by babadbee
I am currently having trouble of reloading a json store with new parameters. Here is my store:
我目前无法使用新参数重新加载 json 存储。这是我的商店:
newsletters = new Ext.data.JsonStore({
url: '/newsletters/',
root: 'results',
fields: [
'id',
'body'
'recipients'
],
baseParams: { command: 'json', to: dateTo, from: dateFrom },
autoLoad: true
});
dateTo and dateFrom are initally empty strings ( '' ) and checking in firebug /newsletters is called with the correct parameters.
dateTo 和 dateFrom 最初是空字符串 ( '' ),并使用正确的参数调用检查 firebug /newsletters。
Now none of the following techniquest work:
现在以下技术都不起作用:
Changing the values of dateTo and dateFrom then calling newsletters.reload() still calls the page with the parameters to and from being empty strings.
更改 dateTo 和 dateFrom 的值然后调用 newsletters.reload() 仍然调用带有参数 to 和 from 为空字符串的页面。
Calling newsletters.reload( { to: 'test1', from: 'test2' } );still sees the parameters as empty strings.
调用newsletters.reload( { to: 'test1', from: 'test2' } );仍然将参数视为空字符串。
Finally as from the manual I have tried:
最后根据我尝试过的手册:
lastOptions = newsletters.lastOptions;
Ext.apply(lastOptions.params, {
to: 'test1',
from: 'test2'
});
newsletters.reload(lastOptions);
This again does not request /newsletters with the updated parameters.
这再次不会请求 /newsletters 具有更新的参数。
Any advice appreciated!
任何建议表示赞赏!
回答by Mchl
You can actually pass params object to the load() method
您实际上可以将 params 对象传递给 load() 方法
newsletters.load({
params: {to: 'test1', from: 'test2'}
})
回答by Drasill
From the docs, you can probably do :
从文档中,您可能可以执行以下操作:
store.setBaseParam('to', dateTo);
Now, if I understand correctly, you want your baseParams to be changed whenever dateTo and dateFrom are changed.
现在,如果我理解正确,您希望在更改 dateTo 和 dateFrom 时更改 baseParams。
You could try :
你可以试试:
var dateTo = '', dateFrom = '';
store.on('beforeload', function(s) {
s.setBaseParam('to', dateTo);
s.setBaseParam('from', dateFrom);
});
// This should work :
dateTo = 1;
dateFrom = 2;
store.load();
回答by Francisco Spaeth
My problem was: I have a store that shall request data over a proxy to back-end. This request shall hold a parameter named filter, which will just help back-end to decide which is the set of result the client is interested in. This parameter is loaded from a Comboboxor some other component which the user can use to express which filter shall be used.
我的问题是:我有一个商店,它将通过代理请求数据到后端。此请求应包含一个名为 的参数filter,该参数将帮助后端确定客户端感兴趣的结果集。此参数是从Combobox用户可以用来表示应使用哪个过滤器的某个或其他组件加载的用过的。
From my point of view the parameters shouldn't be set to the Store and neither using the load parameter. I will explain why:
从我的角度来看,参数不应设置为 Store,也不应使用 load 参数。我将解释原因:
- Configuring parameters to the store would imply that every other component using the same store will have this parameters configured, meaning you can have concurrence problems.
- And on the second case, it is not interesting to have it configurable over load method, because you don't wanna every single time explicit make use of the
loadmethod by your self, remember that there are already some components like paging and custom components that triggers this method.
- 为商店配置参数意味着使用同一商店的每个其他组件都将配置此参数,这意味着您可能会遇到并发问题。
- 在第二种情况下,让它可配置重载方法并不有趣,因为您不想每次都明确地自己使用该
load方法,请记住已经有一些组件,如分页和自定义组件触发此方法。
What would be the right way from my point of view:
从我的角度来看,正确的方法是什么:
Every time that a loadis triggered, we just attach the additional parameter in a non-intrusive way. Meaning that the trigger will not need to have any change (remember here trigger could be any component that executes store.load()) and the store shall be not aware about this new parameter.
每次load触发 a 时,我们只是以非侵入性的方式附加附加参数。这意味着触发器不需要有任何更改(记住这里触发器可以是任何执行的组件store.load())并且商店不应知道这个新参数。
You can see here clearly that this shall be an operation done before request data to proxy, and in my case I implemented as a listener for the beforeloadevent. When beforeloadis executed, I just aggregate the new parameters to the operation parameter of the listener that according documentationis: beforeload( store, operation, eOpts ). The final implementation is something like:
您可以在这里清楚地看到,这应该是在向代理请求数据之前完成的操作,在我的情况下,我将其实现为beforeload事件的侦听器。当beforeload被执行时,我只是聚集新的参数给听者,根据操作参数文件是:beforeload( store, operation, eOpts )。最终的实现是这样的:
store.on({
beforeload: function (store, operation, opts) {
Ext.apply(operation, {
params: {
filterName: Ext.getCmp('filterCombo').getValue()
}
});
}
});

