简体   繁体   English

ExtJS网格 - 如何在视图中按ID引用商店

[英]ExtJS Grid - how do I reference a store by id in my view

I am trying to reference a store in my app for the purpose of adding a paging tool bar at the bottom of my gird. 我试图在我的应用程序中引用一个商店,目的是在我的网格底部添加一个分页工具栏。 In most examples I have studied the store is referenced by variable, ex: store: someStore. 在大多数示例中,我研究过商店是由变量引用的,例如:store:someStore。 However, by I have build my app a little differently and did create a reference variable to the store. 但是,通过我构建我的应用程序有点不同,并确实为商店创建了一个引用变量。 I have tried assigning an id but this did not work. 我尝试过分配一个id,但这不起作用。

Here is what I have: 这是我有的:

In my view Grid.js: 在我看来Grid.js:

Ext.define('myApp.view.user.Grid', {
    extend: 'Ext.grid.Panel',
    viewModel: {
        type: 'user-grid'
    },
    bind: {
        store: '{users}',
    },
    columns: {...},

    //my paging tool bar
    dockedItems: [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store: 'girdStore'
        //store: {users} -> did not work
    }],
    ...
});

In my view model GridModel.js: 在我的视图模型GridModel.js中:

Ext.define('myApp.view.user.GridModel', {
    extend: 'Ext.app.ViewModel',

    requires: [
        'myApp.model.User'
    ],

    stores: {
        users: {
            model: 'myApp.model.User',
            storeId: 'gridStore',
            autoLoad: true
        }
    },
    formulas: {...}
});

When I try to reference the {users} store by id 'gridStore' I get this error: 当我尝试通过id'strestStore'引用{users}商店时,我收到此错误:

Uncaught TypeError: Cannot read property 'on' of undefined

What is the best way to proceed without completely refactoring my model? 没有完全重构我的模型,最好的方法是什么?

When you have a reference to your grid, you could get the store by calling the getStore function. 当您引用网格时,可以通过调用getStore函数来获取存储。 See the ExtJs 6.2.1 documentation . 请参阅ExtJs 6.2.1文档

var grid; // reference to your grid
var store = grid.getStore();

You can create the store in initComponent and then attach it to the dockedItems , so both will share the same store. 您可以在initComponent创建存储,然后将其附加到dockedItems ,这样两者将共享同一个存储。

initComponent: function () {
    var store = Ext.create('Ext.data.Store', {
        model: 'myApp.model.User',
        storeId: 'gridStore',
        autoLoad: true
    });
    this.store = store;
    this.dockedItems = [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store:store
    }];
    this.callParent(arguments);
}

The initComponent is called once when a new instance of the class is created, see the description in the documentation . 创建类的新实例时,将调用initComponent一次,请参阅文档中的说明

...It is intended to be implemented by each subclass of Ext.Component to provide any needed constructor logic. ...它旨在由Ext.Component的每个子类实现,以提供任何所需的构造函数逻辑。 The initComponent method of the class being created is called first, with each initComponent method up the hierarchy to Ext.Component being called thereafter. 首先调用正在创建的类的initComponent方法,然后将层次结构中的每个initComponent方法调用到Ext.Component。 This makes it easy to implement and, if needed, override the constructor logic of the Component at any step in the hierarchy. 这使得它易于实现,并且如果需要,可以在层次结构中的任何步骤覆盖Component的构造函数逻辑。 The initComponent method must contain a call to callParent in order to ensure that the parent class' initComponent method is also called... initComponent方法必须包含对callParent的调用,以确保同时调用父类的initComponent方法...

The view with the initComponent function. 使用initComponent函数的视图。

Ext.define('myApp.view.user.Grid', {
    extend: 'Ext.grid.Panel',
    viewModel: {
        type: 'user-grid'
    },
    initComponent: function () {
        var store = Ext.create('Ext.data.Store', {
            model: 'myApp.model.User',
            storeId: 'gridStore',
            autoLoad: true
        });
        this.store = store;
        this.dockedItems = [{
            xtype: 'pagingtoolbar',
            dock: 'bottom',
            store: store
        }];
        this.callParent(arguments);
    },
    columns: {...},
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM