将字段动态设置为 extjs 数据存储 [英] dynamically set fields to a extjs data store

查看:21
本文介绍了将字段动态设置为 extjs 数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字段动态设置为 extjs 数据存储,以便我可以在运行时动态创建不同的网格.

I am trying to dynamically set fields to a extjs data store so that I could dynamically create a different grid at run time.

案例 A 对我有用.但是当我在案例 B 中使用 as 时,商店的代理挂在以前的模型上,因此网格渲染混乱.

Case A works for me. But when I use as in Case B, the store's proxy hangs on to the previous model and so the grid rendering is messed up.

这两者之间的真正区别是什么?

What is the really the difference between these two?

案例 A

Ext.define('FDG.store.reading.FDGDynamicGridStore', {
    extend: 'Ext.data.Store'
});

var fdgstore = Ext.create('FDG.store.reading.FDGDynamicGridStore', {
        fields: fields,
        proxy: {
            type: 'memory',
            reader: {
            type: 'json',
            totalProperty: 'tc',
            root: 'Result'
            }
        }
        });
fdgstore.loadRawData(output);
this.reconfigure(fdgstore, columns);

案例 B

Ext.define('FDG.store.reading.FDGDynamicGridStore', {
    extend: 'Ext.data.Store',    
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            totalProperty: 'tc',
            root: 'Result'
        }
    }
});

var fdgstore = Ext.create('FDG.store.reading.FDGDynamicGridStore', {
        fields: fields
        });
fdgstore.loadRawData(output);
this.reconfigure(fdgstore, columns);

推荐答案

这是我的想法:

Ext.data.Model 负责持有代理和字段.MVC 不鼓励在商店中设置这些属性,尽管这是 Ext-JS MVC 出现之前的唯一方法.

Ext.data.Model is responsible for holding the proxy and field. Setting those properties on a store is discouraged in favor of MVC, though it was the only way before Ext-JS MVC came along.

商店总是使用与模型对象关联的代理.当您将 fields 属性传递给商店时,会使用默认代理创建匿名 Model.您不应该在指定代理时使用它.来自文档

A store always uses the proxy associated with the model object. When you pass in a fields property to a store, an anonymous Model is created with a default proxy. You shouldn't use that when specifying proxies. From the doc

对于任何更复杂的事情,例如指定特定的 id 属性或关联,应为模型配置定义和指定 Ext.data.Model.

For anything more complicated, such as specifying a particular id property or associations, a Ext.data.Model should be defined and specified for the model config.

我的建议是您根据字段动态创建模型,这样您就没有任何匿名模型伏都教.

My suggestion is that you create the models dynamically based on fields so you don't have any of the anonymous Model voodoo.

function createModelWithCustomProxy(fields) {
    return Ext.define('FDG.store.reading.Mymodel' + Ext.id(), {
        extend: 'Ext.data.Model',
        fields: fields,    
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                totalProperty: 'tc',
                root: 'Result'
            }
        }
    }
});

var fdgstore = Ext.create('Ext.data.Store', {
    model: createModelWithCustomProxy(fields);
});
fdgstore.loadRawData(output);
this.reconfigure(fdgstore, columns);

这篇关于将字段动态设置为 extjs 数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆