2013-07-26 32 views
3

所以我panel(可以称之为fooPanel)含有grid(让我们称之为如果fooGrid,其中有一些store)。 fooPanel可以插入一些tabpanel。所以问题是,tabpanel可能包含两个(或更多)fooPanel的实例,并带有一些不同的参数。我认为这个问题很明显。因为面板上的fooGrids具有相同的stores,只要我重新加载一个商店,两个fooGrids正在重新加载。 (因为他们有相同的stores)。有针对这个的解决方法吗?或者我应该限制用户只打开一个fooPanel的实例,每tabpanel问题与同一家商店格Ext JS的

+0

是否有你需要更多的情况下,任何的理由?如果是这样,只需更改每个实例的商店 – sra

+0

是的,这是有原因的。我如何'为每个实例更改商店'? – Dimitri

回答

7

除了每个网格创建一个商店之外,没有简单的解决方案。如果您不想创建商店的多个实例的原因是为了避免多次加载,您可以在代理级别上进行某种缓存。

编辑如何创建多个商店为您的网格实例

您可以创建存储实例(即使用Ext.create('My.Store'))你自己在你的initComponent方法电网:

Ext.define('My.Store', { 
    extend: 'Ext.data.Store' 
    ,fields: ['name'] 
    ,proxy: { 
     type: 'memory' 
     ,reader: 'array' 
    } 
    ,data: [['Foo'],['Bar'],['Baz']] 
}); 

Ext.define('My.Grid', { 
    extend: 'Ext.grid.Panel' 

    ,columns: [{dataIndex: 'name'}] 

    ,initComponent: function() { 
     // /!\ Do that BEFORE calling parent method 
     if (!this.store) { 
      this.store = Ext.create('My.Store'); 
     } 

     // ... but don't forget to call parent method    
     this.callParent(arguments); 
    } 
}); 

// Then I can create multiple grids, they will have their own store instances 

Ext.create('My.Grid', { 
    renderTo: Ext.getBody() 
    ,height: 200 
}); 

Ext.create('My.Grid', { 
    renderTo: Ext.getBody() 
    ,height: 200 
}); 

或者你可以在创建时指定新的商店实例

Ext.create('Ext.grid.Panel', { 
    renderTo: Ext.getBody() 
    ,height: 200 
    ,columns: [{dataIndex: 'name'}] 
    ,store: Ext.create('My.Store') // one instance 
}); 

Ext.create('Ext.grid.Panel', { 
    renderTo: Ext.getBody() 
    ,height: 200 
    ,columns: [{dataIndex: 'name'}] 
    ,store: Ext.create('My.Store') // two instances! 
}); 

但是,就我而言,我通常不打算创建完整的商店定义。我在模型中配置代理,并使用该模型使用内联存储配置(内联配置将转换为它们自己的实例,在Ext4中)。例如:

Ext.define('My.Grid', { 
    extend: 'Ext.grid.Panel' 

    ,columns: [{dataIndex: 'name'}] 

    // inline store configuration 
    ,store: { 
     // The model takes care of the fields & proxy definition 
     model: 'My.Model' 

     // other params (like remoteSort, etc.) 
    } 
}); 

// Now I can create plenty of My.Grid again, that won't interfere with each other 
+0

网格有一个属性商店权利?并且我实例化的每个面板都将具有相同网格的实例,它本身将具有相同的商店。我可以针对同一个网格的不同实例拥有相同商店的不同实例吗?数据存储由ASP.NET以JSON格式提供。你能否提供一些代码来展示为不同的网格实例创建不同的商店实例的例子? – Dimitri

+0

有很多方法可以实现这一点。我在我的答案中添加了一些示例,希望能够匹配您的用例。 – rixo

+0

@rixo感谢联机商店的建议 - 这似乎是一个更好的方法来做到这一点。 – Mark

1

这篇文章可以帮助你

Ext.define('App.store.MyStore', { 
    extend : 'Ext.data.Store', 
    alias : 'store.app-mystore', // create store alias 

    // ... 
}); 

Ext.define('App.view.MyCombo', { 
    extend : 'Ext.form.field.ComboBox', 
    xtype : 'app-mycombo', 

    requires : [ 
     'App.store.myStore' 
    ], 

    // combo config 
    store : { 
     type : 'app-mystore' // store alias; type creates new instance 
    } 
}); 

http://www.sencha.com/forum/showthread.php?284388-ExtJs-4.2-Multiple-instances-of-the-same-Store&p=1040251