2011-05-03 118 views
6

我有一个自定义的数据存储,存储信息是否存储已被加载一次或没有。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.superclass().initComponent.call(this); 
     this.addEvents('load','beforeload'); 
    }, 
    isLoaded : function() { 
     return this.loaded; 
    }, 
    listeners: { 
     'load' : function(store,records,options) { 
      this.loaded = true; 
     } 
    } 
}); 

这工作正常,直到最近我添加一个'beforeload'事件监听器到这个商店的一个实例。 'load'侦听器现在不会被调用。

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 
      } 
     } 
    } 

}); 

我在这里做错了什么?也请让我知道你的建议,以改善

+0

你在哪里加载店吗?我看不到任何autoLoad:true或accountsDataStore.load()选项。 – Swar 2011-05-03 07:38:29

+0

我尝试在点击标签上加载商店。我的意图是,如果商店先前已加载,则不加载商店 - 如果(!accountsDataStore.isLoaded()){ accountsDataStore.load(); }'。 'isLoaded'总是返回false,因为永远不会调用加载监听器。 – Joji 2011-05-03 07:58:54

+0

嗨,我面临同样的问题。你能告诉我你是如何解决这个问题的? – Shekhar 2012-02-10 05:35:32

回答

0

好吧,我认为有一个范围问题。请您尝试一下这种方法:

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; 
     } 
    } 
+2

事实上,负载监听器甚至没有被调用。 – Joji 2011-05-03 08:14:19

+0

改变这一行:this.superclass()。initComponent.call(this);到Ext.example.Store.superclass.initComponent.call(this);因为店铺载入事件已经存在,所以不要添加载入事件。 – Swar 2011-05-03 08:23:48

+0

不幸的是它似乎不是问题 – Joji 2011-05-03 10:43:22

1

为 “beforeload” 事件状态的文档:

“的请求之前触发是一个由新的数据对象,如果 beforeload处理程序返回false,则加载操作将被取消。“

您可以尝试从您的beforeload侦听器返回true以确保加载操作仍在运行。

5

的问题是,你不应该设置对象的原型,除非你真的知道这意味着什么(它会被所有实例共享,并且可以在每个实例的基础上覆盖)

在Ext JS中,我只在原型上设置配置选项,这只是为了方便,可能会被调用者覆盖。

您的第一堂课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; 
    } 
}); 
1

store.totalCount

如果加载,这个属性返回一个数字 其他 此属性为undefined (ExtJS的-4.1.0-RC1)