在ViewController中获取存储

Get store in a ViewController

本文关键字:存储 获取 ViewController      更新时间:2023-09-26

是否可以从ViewController中检索存储?我试图用商店中的按钮动态填充工具栏,但我不知道如何从控制器访问商店。如有任何提示,不胜感激。我的Extjs版本是5.1。

视图:

Ext.define('EurocampingsCMS.view.Foo', {
    extend: 'Ext.toolbar.Toolbar',
    alias: 'widget.foo',
    requires: [
        'EurocampingsCMS.controller.Foo'
    ],
    controller: 'foo',
});

视图控制器:

Ext.define('MyApp.controller.Foo', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.foo',
    control: {
        '#': {
            beforerender: 'onBeforeRender'
        }
    },
    onBeforeRender: function (toolbar, eOpts) {
        //How to get store here??
        store.each(function (record) {
            toolbar.add({
                xtype: 'button',
                itemId: record.get('localeId'),
                text: record.get('label')
            });
        });
    }
});

最好是充分利用MVVM体系结构,并在视图模型stores对象中定义存储。那么就很容易了:

onBeforeRender: function (toolbar, eOpts) {
    var store = this.getViewModel().getStore('yourstorekey');
}

注意:只有当存储的生存期与视图的生存期相同时,这才有可能。如果你需要访问一个使用寿命更长的全局商店,那么给它分配一个storeId并使用获取它

var store = Ext.getStore('theStoreId');

Ext.getStore('storeIdHere')是实现的一种方法