2011-04-22 91 views
28

隨着ExtJS 3.x,我能夠使用商店的「領域」屬性,但它似乎與ExtJS 4我必須絕對使用模型。這很好,但在我的情況下,它不是一個靜態模型,我需要動態定義這些字段,有時需要更改它們。動態模型與ExtJS 4

我可以重新創建一個模型,但我需要使用不同的名稱,因爲它顯然不可能修改現有的模型,既不刪除它。如果我嘗試使用具有相同名稱的Ext.regModel,ExtJS崩潰。

感謝您的幫助!

回答

20

4.1 UPDATE:

作爲更新...在4.1現在是一個靜態方法setFields其可用於定義模型原型字段。它在控制器的init方法中運行良好。

當我這樣做時,我想在模型類中定義一些靜態字段,然後設置一些動態的。不幸的是,新的setFields方法用參數替換了所有字段,但它很容易處理。

此示例使用,其中我的模型和存儲被包括在MVC模式控制器的model陣列和store陣列(我提供以下所用的方便的吸氣劑):

Ext.define('ST.controller.Main', { 
    extend: 'Ext.app.Controller', 

    models: ['User', 'Reference'], 

    stores: ['CurrentUser', 'PermissionRef'], 

    views: ['MainPanel'], 

    init: function() { 
     var me = this; 

     me.getPermissionRefStore().on('load', function(store, records) { 
      var model = me.getUserModel(); 
       // this returns the static fields already defined 
       // in my User model class 
       fields = model.prototype.fields.getRange(); 

      // add the permission options (dynamic fields) to the static fields 
      Ext.each(records, function(permission) { 
       fields.push({name: permission.get('name'), type: 'bool'}); 
      }); 

      // 4.1 method to update the User model fields 
      model.setFields(fields); 

      // now load the current user (it will use the updated model) 
      me.getCurrentUserStore().load(); 

     }); 

    } 

}); 

User模型和CurrentUser商店是創建完全像常規,非動態模型和存儲將被包括在其各自的控制器陣列中,'用戶'模型僅僅缺少如上所示添加的動態字段。

+0

感謝您添加更新 – Jom 2012-05-15 11:38:16

+0

爲什麼不直接將新字段添加到''MixedCollection''字段?是否有'setFields'方法需要處理?我的意思是旁邊創建'Field'實例,在你的情況。 – rixo 2013-07-30 20:37:59

3

下面是一個非常簡單的例子。只需使用一個正常的Ext.data.Store但不是一個模式,指定的字段屬性:

// you can specify a simple string ('totally') 
// or an object with an Ext.data.Field ('dynamic') 
var fields = ['totally', {name : 'dynamic', type : 'string'}]; 
var newStore = new MyApp.store.Object({ 
    fields : fields 
    // other options like proxy, autoLoad... 
}); 

不要指定模型屬性 - 它似乎會覆蓋領域的財產。

我也想改變列和動態現有電網的內容:關於Ext.data.Store的「域」屬性

// reconfigure the grid to use the new store and other columns 
var newColumns = [ 
    {header: 'Totally', dataIndex: 'totally'}, 
    {header: 'Dynamic', dataIndex: 'dynamic'} 
]; 
myGrid.reconfigure(newStore, newColumns); 

從Ext JS的4文件:

這可能是用於代替指定模型配置的 。 字段應該是一組 Ext.data.Field配置對象。 商店將自動創建一個帶有這些字段的 Ext.data.Model。在 通用這個配置選項 應該避免,它存在的目的是向後兼容。 對於任何更復雜的操作,例如 指定了一個特定的id屬性或 關聯,應爲 配置定義並指定一個Ext.data.Model 。

所以要小心 - Sencha可能會在將來刪除它。

+0

這裏小心 - 如果您在商店中設置字​​段,而忽略模型,並且您有自定義ID字段,則會失敗。您的商店記錄將設置其Id,但無法通過getId()返回Id,因爲他們不知道自定義ID。不知道這是否是一個錯誤,但無論如何我都會報告它http://www.sencha.com/forum/showthread.php?280160-model.getId()-with-custom-IdProperty-fails-when-and-store -fields-set-via-metaData&p = 1024289&viewfull = 1#post1024289 – HDave 2014-01-21 16:56:28

+0

這是一個爲我工作的人...不得不刪除「Model」,而是將其配置爲hardcore .. – PedroMorgan 2014-02-05 13:36:29

17

我也進入了這個問題。我有一個服務負責從服務器獲取元數據,並將模型和存儲適配到這個元數據。

因此,我定義了一個空模型並將商店配置爲使用此模型。

處理元數據時,我將這些新/附加字段添加到模型原型中(metaDataStore是包含元數據的存儲區,模型是可以從模型管理器獲取的模型):

var fields = []; 
metaDataStore.each(function(item) { 
    fields.push(Ext.create('Ext.data.Field', { 
     name: item.get('field') 
    })); 
}); 
model.prototype.fields.removeAll(); 
model.prototype.fields.addAll(fields); 

當我然後調用使用此模型的商店的負載或創建新的模型實例時,正確處理新的字段。