2012-04-16 72 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