在 ViewController 中获取商店

Get store in a ViewController

是否可以从 ViewController 中检索商店? 我正在尝试使用商店中的按钮动态填充工具栏,但我不确定如何从控制器访问商店。 任何提示表示赞赏。 我的 Extjs 版本是 5.1.

查看:

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

ViewController:

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

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

最好是充分利用 MVVM 架构并在视图模型 stores 对象中定义商店。那么就简单了:

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

注意:只有当商店的生命周期与视图的生命周期相同时,这才有可能。如果您需要访问寿命更长的全局商店,请为其分配一个 storeId 并通过以下方式获取它:

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