Extjs两个不使用不同模型的商店实例

时间:2014-05-02 19:56:20

标签: extjs components instance

修改 http://jsfiddle.net/zns6B/4/添加了js小提琴链接并遇到了无法获取的字段'现在未定义

edit2 所以我发现Sc.Model.B的第二个网格存储模型是正确的。但商店中的记录包含Sc.Model.A的ID。所以我的商店模型设置为Sc.Model.B但商店使用的是Sc.Model.A。它仍然将数据存储在商店中,但只是模型首先设置为Sc.Model.A。

edit3 我已经从ListGrid中创建了所有实例。我在创建列表网格时添加了它们。我添加了以下内容。这也不起作用。我不知道该做什么。

var obj1 = Ext.create('Sc.ListGrid',{
        title: "first Component",
        foo: true,
        id: 'firstGrid',
        myStore: Ext.create('My.Store.MyStore',{model:Ext.create('My.Model.Model'});
        renderTo: 'renderToMe1'
    });

我正在尝试生成这两个网格。当foo == true时,我希望它生成一个模型A的商店。当它等于false时,我希望它使用模型B.我试图只是专门添加My.Model.MyModel,但这不起作用。第二个网格将以某种方式继承第一个模型模型。我改变它只是为了尝试使用字段而不是使用模型,但第二个网格仍然使用第一个网格。

我也试过在initComponent中声明存储,但是我得到了相同的结果。

    var obj1 = Ext.create('Sc.ListGrid',{
        title: "first Component",
        foo: true,
            id: 'firstGrid',
        renderTo: 'renderToMe1'
    });

var obj2 = Ext.create('Sc.ListGrid',{
        title: "second Component",
        foo: false,
        renderTo: 'renderToMe2'
    });

Sc.ListGrid

Ext.define('Sc.ListGrid', {
    extend: 'Ext.grid.Panel',
    title: 'Grid',
    store: Ext.data.StoreManager.lookup('bleh'),
    requires: ['stuff'],
    columns: [
        { text: 'id',  dataIndex: 'id' },
    ],
    config:{
        foo: null,
    },
    initComponent: function(){

        if(this.foo == true){
            Ext.apply(this,{
                store: this.buildStore1()
                });
        }
        if(this.foo == false){
            Ext.apply(this,{
                store: this.buildStore2()
                });
        }
        this.callParent();
    },
    buildStore1:function(){
        return Ext.create('Sc.Store.League.LeagueStore',{url:'somewhere',fields:["S"]});
    },
    buildStore2:function(){
        return Ext.create('Sc.Store.League.LeagueStore',{url:'somewhere',fields:["A"]});
    }
});

也是我尝试使用的模型示例。

 Ext.define('Sc.Model.A', {
      extend: 'Ext.data.Model',
      fields: [
          {name: 'id',  type: 'string'},
          {name: 'type',   type: 'string'},
          {name: 'gamename', type: 'string'}
       ]
});

 Ext.define('Sc.Model.B', {
          extend: 'Ext.data.Model',
          fields: [
              {name: 'id',  type: 'string'},
              {name: 'type',   type: 'string'},
              {name: 'gamename', type: 'string'},
              {name: 'something1', type: 'string'},
              {name: 'something2', type: 'string'},
           ]
    });

它将创建两个网格并从我的webservice加载数据。当我用Sc.Model.B的数据检查网格时,它将具有id和type。但是没有任何关于something1和something2的数据。我的webserivce返回json并输入所有值。没有空值。如果我是Ext.getCmp(' firstGrid')。getStore()。getData(0);如果我使用Ext.getCmp(' firstGrid')。getStore()并检查模型名称。它显示了模型B,但反映了A

2 个答案:

答案 0 :(得分:0)

您是否需要在initComponent() ??

中完成

这是我前一段时间保存的小提琴,当时我正在尝试做类似的事情。如果您需要帮助调整它,请告诉我并更新它。

需要注意的主要是grid.reconfigure(store,columns);

这将适当地改变网格的存储和列。

http://jsfiddle.net/zqG55/1/

答案 1 :(得分:0)

问题在于代理未正确设置或创建,因为代理模型引用了以前的模型实例。这是我的解决方案

var themodel = 'A.Model.SomeModel';

var myProxy = new Ext.data.proxy.Ajax({
            model: themodel,
            url: url,
            reader: {
                type: 'json',
            }
        });

Ext.apply(this,{
                columns: modelColumns.columns,
                store: Ext.create('M.Store.MyStore',{
                        model: themodel ,
                        autoLoad: true,
                        proxy: myProxy
                    })
                });