简体   繁体   English

扩展Ext.data.Store

[英]Extending Ext.data.Store

Here is the super class: 这是超类:

Ext.define('My.store.Direct', {
    extend: 'Ext.store.Direct',
    paramsAsHash: true,
    proxy: {
        reader: {
            type: 'json',
            root: 'data',
            totalProperty: 'total'
        },
        type: 'direct',
    }
});

In subclasses I only need to set the API config. 在子类中,我只需要设置API配置。 I tried: 我试过了:

Ext.define('My.store.User', {
        extend: 'My.store.Direct',
        model: 'My.model.User',
        storeId: 'user',
        constructor: function () {
            this.callParent(arguments);
            this.getProxy().api.read = RPC.UserApi.read;
        }}
);

Ext.define('My.store.Post', {
        extend: 'My.store.Direct',
        model: 'Mehr.model.Post',
        storeId: 'post',
        constructor: function () {
            this.callParent(arguments);
            this.getProxy().api.read = RPC.PostApi.read;
        }}
);

Unfortunately, after executing the code both store will have the api.read of the first subclass. 不幸的是,执行代码后,两个存储区都将具有第一个子类的api.read What is wrong with it? 怎么了 What is proper method of extending stroes? 延长杆的正确方法是什么?

Create your proxy in constructor, otherwise proxy goes to the prototype of My.store.Direct and because it is an object, not a primitive type (int, string, bool), it is accessed by reference. 在构造函数中创建代理,否则代理将转到My.store.Direct的原型,由于它是一个对象,而不是原始类型(int,string,bool),因此可以通过引用进行访问。 Thus, changing proxy in one instance changes all instances - there is only one proxy in fact. 因此,在一个实例中更改代理会更改所有实例-实际上只有一个代理。

So base class should be defined similar to this 因此,基类的定义应与此类似

Ext.define('My.store.Direct', {
    extend: 'Ext.data.DirectStore',
    paramsAsHash: true,
    constructor:function(config) {
      Ext.merge(config, {proxy: {
        reader: {
            type: 'json',
            root: 'data',
            totalProperty: 'total'
        },
        type: 'direct'
    }
    });
        this.callParent([config]);
    }
});

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

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