2011-12-02 89 views
5

如何創建獨特的商店實例並將其分配給視圖(如果需要,我可以創建獨特的視圖和/或控制器)?Ext JS 4 - 如何創建多個商店實例並分配給視圖? (MVC)

一個簡單的用例 - 我想打開多個網格(相同類型)與每個商店中的記錄列表。每個網格都需要有它自己的商店實例,因爲它可以擁有自己的記錄列表,它自己的過濾等等。

我試過了,但它不起作用,我的網格沒有繪製:

var theView = Ext.create('App.view.encounter.List'); 
    theView.title = 'WORC Encounters'; 
    var theStore=Ext.create('App.store.Encounters'); 
    theView.store=theStore; 
    tabhost.add({title:'WORC',items:theView}); 

    var theView = Ext.create('App.view.encounter.List'); 
    theView.title = 'NC Encounters'; 
    var theStore2=Ext.create('App.store.Encounters'); 
    theView.store=theStore2; 
    tabhost.add({title:'NC',items:theView}); 
+0

嗨,即使我在類似的情況。你有沒有找到任何答案? – Shekhar

+0

不,不幸的是,由於這個和其他幾個原因,我已經把我的extjs開發擱置了,並且正在使用一個不同的平臺(基於非js)。 –

+0

接受答案。 –

回答

3

您需要在組件初始化時(或之前)分配存儲。在initComponent中。

Ext.define('classname', { 
    extend: 'Ext.grid.Panel', 
    //... 
    initComponent: function() { 
     var me = this; 

     var theStore = Ext.create('App.store.Encounters'); 
     Ext.apply(me, { 
      store: theStore 
     }); 

     me.callParent(); 
    } 
    //... 
}); 

你也可以這樣來做:

//Create the store 
var theStore = Ext.create('App.store.Encounters'); 
//Create the view 
var theView = Ext.create('App.view.encounter.List', { 
    store: theStore 
}); 

編輯爲您具體的例子:

var theStore = Ext.create('App.store.Encounters'); 
var theView = Ext.create('App.view.encounter.List', { 
     title: 'WORC Encounters', 
     store: theStore 
}); 
tabhost.add({title:'WORC',items:theView}); 


var theStore2=Ext.create('App.store.Encounters'); 
var theView2 = Ext.create('App.view.encounter.List', { 
     title: 'NC Encounters', 
     store: theStore2 
}); 
tabhost.add({title:'NC',items:theView2}); 
+0

我看到1個商店,那麼第二個在哪裏? –

+0

您只需在另一個視圖中定義第二個商店,這與工作方式相同。在相同視圖中定義第二個商店是沒有用的...... –

+0

好吧,所以爲了幫助我理解,您的代碼和我的代碼之間的區別看起來像是「Ext.apply」,對嗎? –