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

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

问题描述



案例A

我正在尝试动态地将字段设置为extjs数据存储,以便我可以在运行时动态创建一个不同的网格。强>为我工作但是当我使用案例B 时,商店的代理挂起到以前的模型,因此网格渲染被弄乱了。



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



案例A

  Ext.define('FDG.store.reading.FDGDynamicGridStore',{
extends:'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',{
extends:'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模型负责持有代理和字段。不要在商店中设置这些属性,尽管它是Ext-JS MVC之前的唯一方法,但是不喜欢MVC。



一个商店总是使用与模型对象。当您将字段属性传递到商店时,将使用默认代理创建匿名模型指定代理时不应该使用它。从文档


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


我的建议是,您可以根据字段动态创建模型,这样您就不会有任何匿名的模特巫毒。

  function createModelWithCustomProxy(fields){
return Ext.define('FDG。 store.reading.Mymodel'+ Ext.id(),{
extends:'Ext.data.Model',
fields:fields,
proxy:{
type:'记忆',
阅读器:{
type:'json',
totalProperty:'tc',
root:'Result'
}
}
}
});

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


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

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?

Case 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);

Case 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);

解决方案

Here's what I think is going on:

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.

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

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天全站免登陆