未调用ExtJS存储加载侦听器
我有一个自定义数据存储,它存储信息,无论存储是否加载过一次未调用ExtJS存储加载侦听器,extjs,Extjs,我有一个自定义数据存储,它存储信息,无论存储是否加载过一次 /** * Custom DataStore which stores the information whether data has been loaded once or not. */ Ext.example.Store = Ext.extend(Ext.data.Store, { loaded: false, initComponent: function() { this.supercla
/**
* Custom DataStore which stores the information whether data has been loaded once or not.
*/
Ext.example.Store = Ext.extend(Ext.data.Store, {
loaded: false,
initComponent: function() {
this.superclass().initComponent.call(this);
this.addEvents('load','beforeload');
},
isLoaded : function() {
return this.loaded;
},
listeners: {
'load' : function(store,records,options) {
this.loaded = true;
}
}
});
直到最近,我才将“beforeload”事件侦听器添加到此存储的一个实例中。“加载”侦听器现在未被调用
var accountsDataStore = new Ext.example.Store({
id : 'account',
proxy : new Ext.data.HttpProxy({
url : 'app/account/fetch/all',
method : 'post',
api : {
destroy : 'app/account/delete'
}
}),
reader : accountReader,
writer : accountWriter,
remoteSort : true,
sortInfo : {
field : 'createdOn',
direction : "DESC"
},
listeners: {
beforeload: function(store, options) {
var sort = options.params.sort;
// setting the sort parameters to match the property names in the DTO
switch(sort) {
case 'company' :
options.params.sort = 'company.name';
break;
default:
// do nothing
}
}
}
});
我在这里做错了什么?另外,请告诉我您对改进的建议。好的,我认为范围有问题。请这样尝试:
listeners: {
'load' : {
fn : function(store,records,options) {
console.log(this, this.loaded);
this.loaded = true;
},
scope : this
}
}
或者您可以使用:
listeners: {
'load' : function(store,records,options) {
store.loaded = true;
}
}
“加载前”事件的文档说明:
“在请求新数据对象之前激发。如果
在ForeLoad处理程序返回false之前,加载操作将被取消。“
您可以尝试从beforeload侦听器返回true,以确保加载操作仍在运行。问题是,除非您真正知道它的含义(它将由所有实例共享,并且可以在每个实例的基础上重写),否则决不应在原型上设置对象 在ExtJS中,我只在原型上设置配置选项,这些选项只是为了方便起见,可能会被调用方覆盖 您的第一个类
Ext.example.Store
将侦听器放在其原型上。
然后,通过将侦听器对象传递到配置中,在accountsDataStore中覆盖它
要解决问题,只需从构造函数调用this.on('event')
,而不是在原型上设置侦听器
/**
* Custom DataStore which stores the information whether data has been loaded once or not.
*/
Ext.example.Store = Ext.extend(Ext.data.Store, {
loaded: false,
constructor: function() {
this.superclass().constructor.call(this);
// No need to call this, you're not adding any events
// this.addEvents('load','beforeload');
this.on('load', function(store,records,options) {
this.loaded = true;
}, this);
},
isLoaded : function() {
return this.loaded;
}
});
store.totalCount
如果已加载,此属性将返回一个数字
其他的
此属性未定义
(extjs-4.1.0-rc1)您在哪里加载商店?我看不到任何自动加载:true或accountsDataStore.load()选项。我尝试通过单击选项卡加载存储。如果先前已加载存储,则我的目的不是加载存储-
如果(!accountsDataStore.isLoaded()){accountsDataStore.load();}
。isLoaded
总是返回false,因为从未调用加载侦听器。您好,我也面临同样的问题。你能告诉我你是如何解决这个问题的吗?事实是负载侦听器甚至没有被调用。更改这一行:this.superclass().initComponent.call(this);调用Ext.example.Store.superclass.initComponent.call(this);不要添加加载事件,因为存储加载事件已经存在。不幸的是,这似乎不是问题所在。当我关闭beforeload侦听器时,控件进入加载侦听器。