ExtJS的动态模型4 [英] Dynamic Model with ExtJS 4
问题描述
我可以重新创建一个模型,但是我需要使用一个不同的名称,因为显然不可能修改一个现有的模型,也不会删除它。如果我尝试使用同名的Ext.regModel,ExtJS会崩溃。
感谢您的帮助!
4.1更新:
作为4.1中的更新...现在有一个静态方法 setFields
可用于定义模型原型字段。它在控制器的init方法中运行良好。
当我这样做时,我想在模型类中定义一些静态字段,然后再设置一些动态。不幸的是,新的 setFields
方法替换所有字段的参数,很容易处理。
此示例使用MVC模式,其中我的模型和存储包含在控件的模型
数组和 store
array(给我下面使用的方便的getter):
Ext.define('ST.controller.Main',{
extends:'Ext.app.Controller',
models:['User','Reference'],
存储:['CurrentUser',' PermissionRef'],
视图:['MainPanel'],
init:function(){
var me = this;
me.getPermissionRefStore()。on('load',function(store,records){
var model = me.getUserModel();
//这返回已定义的静态字段
//在我的用户模型类
fields = model.prototype.fields.getRange ();
//向静态字段添加权限选项(动态字段)
Ext.each(records,function(permission){
fields.push({name :permission.get('name'),键入:'bool'});
});
// 4.1更新用户模型字段的方法
model.setFields(fields);
//现在加载当前用户(将使用更新的模型)
me.getCurrentUserStore()。load();
});
}
});
用户
模型和 CurrentUser
存储创建完全像常规的非动态模型,存储将被包含在它们各自的控制器阵列中,用户模型简单地缺少如上所示添加的动态字段。
With ExtJS 3.x, I was able to use the "fields" property of a Store, but it seems with ExtJS 4 I have to absolutely use a Model. It's fine, but in my case, it's not a static Model, and I need to define the fields on the fly and sometimes to change them.
I could re-create a Model, but I need to use a different name as it's apparently not possible to modify an exisiting Model, neither delete it. If I try to use Ext.regModel with the same name, ExtJS crashes.
Thanks for your help!
4.1 UPDATE:
As an update... in 4.1 there is now a static method setFields
which can be used to define the model prototype fields. It works well in a controller's init method.
When I did this, I wanted to have some static fields defined in the model class and then set some more dynamically. Unfortunately the new setFields
method replaces all fields with the argument, it was easy enough to handle though.
This example uses the MVC pattern where my model and store are included in the controller's model
array and store
array (providing me with the handy getters used below):
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();
});
}
});
The User
model and CurrentUser
store are created exactly like regular, non-dynamic models and stores would be and included in their respective controller arrays, the 'User' model is simply missing the dynamic fields which are added as shown above.
这篇关于ExtJS的动态模型4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!