2012-04-18 82 views
0

我使用jsonp代理使用商店填充listview,向我的服務器發送GET請求。當應用程序啓動時,請求被髮送,並且列表項與應答一樣呈現。現在當我手動在我的數據庫服務器中添加一個新項目並使用下面的控制器中顯示的代碼刷新列表並檢查控制檯中的存儲對象時,我可以看到新創建的項目位於存儲數據中。迄今如此好,一切都如預期。但是,新項目不在列表中刷新。奇怪的是,當我檢查列表對象及其包含的商店對象時,新創建的項目不包括在內。但是,當我重新啓動應用程序(而不是重新加載商店和刷新列表)新的新創建的項目出現在列表中。對我來說,似乎我有2個商店實例,一個與新數據(我用Ext.getStore檢索)和一個沒有(在ListView中的那個),我認爲應該實際上是相同的。這怎麼可能?下面我向你展示我是如何做到這一點的。任何幫助是極大的讚賞。我有兩個同一商店的實例嗎?

控制器

Ext.define('VB1.controller.ListController', { 
    extend: 'Ext.app.Controller', 
    config: { 
     refs: { 
      listView: 'listview', 
      refreshButtonTap: '#refreshbutton', 
     }, 
     control: { 
      refreshButtonTap: { 
       onRefreshList: 'refreshList', 
      }, 
     }  
    }, 
    launch: function() { 
     this.callParent(arguments); 
     Ext.getStore('MyStore').addListener('load', 'recordLoaded', this); 
    }, 
    refreshList: function() { 
     var loadedStore = Ext.getStore('MyStore').load(); 
     console.log(loadedStore) //here the new item is included 
    }, 
    recordLoaded: function() { 
     var list = this.getListView(); 
     list.refresh(); 
     console.log(list) //here the new item is not included in the store object inside 
            //listobject 
    } 
}); 

商店

Ext.define('VB1.store.MyStore', { 
    extend: 'Ext.data.Store', 
    alias: 'widget.mystore', 
    config: { 
     model: 'VB1.model.MyModel', 
     autoLoad: true, 
     proxy: { 
      type: 'jsonp', 
      url : 'myUrl.js', 
      enablePagingParams: false, 
      reader: { 
       type: 'json', 
       rootProperty: 'array.resources' 
      }, 
     }, 
    }, 
}); 

列表視圖

Ext.define('VB1.view.ListView', { 
    extend: 'Ext.dataview.List', 
    alias: 'widget.listview',   
    requires: [ 
     'Ext.dataview.List', 
    ], 
    scrollable: 'vertical', 
    config: { 
     loadingText: "loading...", 
     emptyText: '</pre><div class="notes-list-empty-text">No notes found.</div><pre>', 
     itemTpl: '</pre><div class="list-item-title">{title}</div><div class="list-item-narrative"><img src="{listlogo}"/></div><pre>', 
    }, 
    listeners: { 
     itemtap: function(list, index, item, record) { 
      this.fireEvent('showDetails', record, this); 
     } 
    }, 
}); 

這是我怎麼添加名單,包括商店視在應用程序啓動

var listView = { 
    xtype: 'listview', 
    store: {xtype: 'mystore'}, 
} 
Ext.Viewport.add(listView); 

回答

1

我剛剛發現問題是什麼。首先,商店被實例化了2次,正如我懷疑的那樣。顯然有一次是由應用程序,另一次是由視圖。所以我結束了兩個商店。因爲我沒有設置storeID,所以商店通過兩個唯一的ID來標識,這兩個ID基於我在商店的別名配置中給出的xtype名稱。

我分辨和記錄在控制器像下面有ID前的MyStore-1

var loadedStore = Ext.getStore('MyStore').load(); 
     console.log(loadedStore) //here the new item was included 

和列表視圖對象內的商店的商店得到了ID EXT-的MyStore-2

var list = this.getListView(); 
    console.log(list._store); //here the new item was not included 

在我看來,改變了鑽孔問題的原因是我在商店中添加了storeId。在那之後,我仍然有兩家商店,但是有着同樣的商店標識(但帶有不同的獨特或可觀察的標識)。好的新功能是現在這個新的商品被包含在兩個商店實例中。我也能夠通過在商品列表視圖中沒有實例化商店的xtype而通過storeID引用它來擺脫第二個商店實例。現在一切都按原樣進行。感謝任何看着這個的人。我希望這能幫助發現類似問題的人。

相關問題