2011-05-03 41 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)