gpt4 book ai didi

javascript - Exj JS 5,从 ViewController 访问存储/模型

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:34 25 4
gpt4 key购买 nike

我想从 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. 如何访问模型并使用其加载方法加载项目?

最佳答案

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');

"A view controller is a controller that can be attached to a specific view instance so it can manage the view and it's child components. Each instance of the view will have a new view controller, so the instances are isolated."

ViewController 必须关联到 View。没有 View ,它就没有任何意义。 ViewController 用于处理与其 View 相关的所有控件:路由、事件处理程序...如果您使用 MVVM 设计,您还应该使用 ViewModel。您的观点(虽然不是强制性的)。如果您已正确设置 MVVM,则可以使用 ViewController 和 ViewModel 已提供的默认方法轻松获取关联商店和模型的实例。

getStore( name ) : Ext.data.Store Get a Ext.data.Store attached to the ViewModel attached to this controller. See Ext.app.ViewModel.getStore.

请检查MVVM architecture .我也找到了 Saki的示例非常有用:http://extjs.eu/ext-examples/#complex-data-binding-5

关于javascript - Exj JS 5,从 ViewController 访问存储/模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com