2012-04-16 75 views
0

我有以下代码。我想根据url.local或url.remote是否被选中来使代理的类型和url属性动态化。如何基于变量的值动态构建ExtJS对象?

var url = { 
    local: './grid-filtering/sample.json', // static data file 
    remote: '/Customer/Get' 
}; 

Ext.require('sbpm.model.Product'); 
Ext.define('sbpm.store.Customer', { 
    extend: 'Ext.data.JsonStore', 
    constructor: function (cfg) { 
     var me = this; 
     cfg = cfg || {}; 
     me.callParent([Ext.apply({ 

      // store configs 

      autoDestroy: true, 
      storeId: 'Customer', 
      model: 'sbpm.model.Product', 
      proxy: { 
       type: 'jsonp', 
       url: url.local, 
       reader: { 
        root: 'data', 
        totalProperty: 'total' 
       } 

      }, 
      remoteSort: false, 
      sorters: [{ 
       property: 'company', 
       direction: 'ASC' 
      }], 
      pageSize: 50 
     }), cfg]); 
    } 
}); 

换句话说,就是我想要做的是指定(在伪代码):

if (url.local) 
{ 
proxy:{ 
    type: 'jsonp' 
    url: url.local, 
    // etc 
} 

} 
else if (url.remote) 
{ 
proxy:{ 
    type: 'rest' 
    url: url.remote, 
    // etc 
} 
} 

我很抱歉,但我不知道是什么样的背景下加入到进一步解释场景,或者如果stackoverflow只是使用某种文本/代码比来衡量,这会很烦人,因为我已经非常简洁地解释了场景,如果人们不了解它,可以提出更详细的问题。

回答

0

AbstractStore类有两种方法setProxy()/getProxy()。您可以使用它们即时切换代理。如果你需要更多的东西 - 比如里面有两个不同的代理并在它们之间切换而不重新创建 - 你可能需要扩展现有的类。

+0

完美。谢谢沙!你只是看看它。我觉得如此n00b;我应该知道的。 – rivanov 2012-04-16 17:55:49

+0

是的。我只是看着源头。 ExtJs有很好的结构和良好的评论源代码:)当有疑问 - 看看那里。 – sha 2012-04-16 17:58:31