網站搜尋

javascript - Exj JS 5,從 ViewController 訪問 Store/Model


我想從 ViewController 載入項目/項目。這裡有一個範例:

Ext.define('MyApp.model.User', {
   extend: 'Ext.data.Model',
   fields: ['name', 'email'],
   proxy: {
       type: 'rest',
       ...
   }
});

Ext.define('MyApp.store.User', {
   extend: 'Ext.data.Store',
   model: 'MyApp.model.User',
   data : [
    {firstName: 'Seth',    age: '34'},
    {firstName: 'Scott', age: '72'},
    {firstName: 'Gary', age: '19'},
    {firstName: 'Capybara', age: '208'}
   ]
});

Ext.define('MyApp.view.MainController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.main',

    init: function() {
        //how to access store here and load items, using its load() method?
        //how to access model here and load an item, using its load() method?
    }
});
  1. 如何從這裡訪問商店並使用它的 load() 方法加載項目?
  2. 如何使用模型的載入方法來存取模型並載入一個項目?

答案#1

this.getView().getStore() // get the store from the controller's view
this.getStore('name') // get store by name
this.getViewModel.getStore('store key') // get store from the View model using the key of the store

你甚至可以做,

// You can use these from anywhere in the application 
Ext.getStore('Store name');
Ext.data.StoreManager.lookup('Store name');
AppName.app.getModel('Model Name').create();
Ext.ClassManager.get('MyModel');
Ext.data.schema.Schema.instances.default.getEntity('MyModel');

「視圖控制器是一種可以附加到特定視圖實例的控制器,因此它可以管理該視圖及其子組件。每個視圖實例都會有一個新的視圖控制器,因此這些實例是相互隔離的。」

一個 ViewController 必須與一個 View 關聯。沒有 View,它沒有什麼重要用途。ViewController 用來處理與其 View 相關的所有控制:路由、事件處理器…… 如果你使用 MVVM 設計,你也應該為你的 View 使用一個 ViewModel(雖然這並非強制)。如果你正確設置了你的 MVVM,那麼你可以很容易地使用 ViewController 和 ViewModel 已提供的默認方法來獲取相關的 stores 和 models 實例。

getStore( name ) : Ext.data.Store 獲取附加到此控制器的 ViewModel 的 Ext.data.Store。請參見 Ext.app.ViewModel.getStore。

請檢查 MVVM 架構。我也覺得 Saki 的範例非常有用:http://extjs.eu/ext-examples/#complex-data-binding-5