【发布时间】:2015-11-30 00:49:10
【问题描述】:
我正在尝试使用 Ext.state.CookieProvider 存储我的网格状态。问题是当状态本身(宽度、顺序)正常恢复时,我无法恢复分拣机参数。
首先我在视口视图控制器的 init() 方法中创建了 cookieprovider:
Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', {}));
我的商店设置为使用远程排序自动加载:
Ext.define('MyApp.requests.store.QueryRequestsGridStore', {
extend: 'Ext.data.Store',
model: 'MyApp.requests.model.QueryRequestsGridModel',
alias: 'store.queryRequestsGrid',
remoteSort: true,
autoLoad: true,
proxy: {
startParam: 'offset',
limitParam: 'limit',
url: '/requests',
noCache: false,
type: 'ajax',
reader: {
type: 'json',
rootProperty: 'data',
totalProperty: 'total'
}
},
});
Store 是使用 viewmodel 绑定在网格中定义的:
bind: {
store: '{queryRequestsGrid}'
},
我在按钮点击时从视口视图控制器加载包含商店的网格,如下所示:
var panelToAddName = Ext.create('MyApp.requests.view.QueryRequestsGridView', {});
var mainViewPort = Ext.ComponentQuery.query('#mainViewPort')[0];
var regionPanel = mainViewPort.down('[region=center][xtype=panel]');
regionPanel.removeAll();
regionPanel.add(panel);
Cookie 包含排序器,但加载网格时没有任何排序参数。
"storeState":{"sorters":[{"root":"data","property":"date_completed","direction":"ASC"}]}}}
我深入研究了 ext-all-debug.js 源文件,发现了“Ext.state.Stateful”类的 initState() 方法。
initState: function() {
var me = this,
id = me.stateful && me.getStateId(),
hasListeners = me.hasListeners,
state, combinedState, i, len, plugins, plugin, pluginType;
if (id) {
combinedState = Ext.state.Manager.get(id);
if (combinedState) {
state = Ext.apply({}, combinedState);
if (!hasListeners.beforestaterestore || me.fireEvent('beforestaterestore', me, combinedState) !== false) {
plugins = me.getPlugins() || [];
for (i = 0 , len = plugins.length; i < len; i++) {
plugin = plugins[i];
if (plugin) {
pluginType = plugin.ptype;
if (plugin.applyState) {
plugin.applyState(state[pluginType], combinedState);
}
delete state[pluginType];
}
}
me.applyState(state);
if (hasListeners.staterestore) {
me.fireEvent('staterestore', me, combinedState);
}
}
}
}
},
如果从该方法内部记录 me.store,则存储在控制台中显示为 ext-empty-store 而 me 是我加载的网格。似乎在正确加载商店之前正在应用状态。
如果在 beforerender 网格事件中重用 initState 方法,排序器将正确地从 cookie 中恢复。
有什么建议吗?
【问题讨论】:
-
我建议制作一个非常简单的煎茶小提琴(带有单列的网格,加载带有单个字段的商店等),这会出现问题。然后你也把它作为一个错误发布在煎茶论坛上。
-
我刚刚还注意到一件事:您可以将代码发布到您将商店添加到网格的位置/方式,以及将状态添加到网格的位置/方式。有几种方法可以添加商店(
store:'MyStoreId'与store:Ext.create('MyApp.store.MyStore'),这可能会表现出不同的行为。 -
@Alexander,我已经添加了你建议的代码,它有帮助吗?如果没有,我会做一个煎茶小提琴。
标签: extjs