2015-04-25 75 views
1

如何訪問和設置視圖模型內定義的商店?我可以設置在視圖模型之外創建的商店。但是我想讓商店保存在視圖模型中,併爲該網格設置該商店。Aceess extjs viewmodel商店視圖

查看

Ext.define('MyApp.view.sample.Sample', { 
extend: 'Ext.panel.Panel', 

requires: [ 
    'MyApp.view.sample.SampleController', 
    'MyApp.view.sample.SampleModel' 
], 

xtype: 'app-sample', 
controller: 'sample', 

viewModel: { 
    type: 'sample' 
}, 
items: [{ 
    xype: 'container', 
     items: [ 
      { 
       xtype: 'grid', 
       store: 'sample', //This doesn't work. Store undefined error is thrown 
       columns: [ 
        { 
         text: 'Name', 
         dataIndex: 'name' 
        } 
       ] 
      } 
     ] 
    } 
] 

});

視圖模型

Ext.define('MyApp.view.sample.SampleModel', { 
extend: 'Ext.app.ViewModel', 
alias: 'viewmodel.sample', 
stores: { 
    sample: { 
     // storeId: 'sample', //doesn't work even if I uncomment this 
    fields: [ 'name' ], 
     data: [ 
      { name: 'Lisa' }, 
      { name: 'Bart' }, 
      { name: 'Homer',} 
     ] 
    } 
} 
}); 

回答

2

你需要綁定你的店(這需要從一個視圖模型所採取的所有特性做),並使用正確的「綁定」符號http://www.sencha.com/forum/showthread.php?284474-ViewModel-stores-help

xtype: 'grid', 
bind: { 
    store: '{sample}' // curly braces if referencing from ViewModel 
}, 
columns: [ 
... 
] 

你是使用ExtJS 5,正確嗎?我只提到,因爲你的一個標籤是'extjs4',ViewModel直到ExtJS 5才被引入。

+0

是的,我使用的是ExtJS 5.謝謝 – user43286