ExtJS 4 动态模型 [英] Dynamic Model with ExtJS 4

查看:18
本文介绍了ExtJS 4 动态模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ExtJS 3.x 中,我可以使用 Store 的fields"属性,但似乎在 ExtJS 4 中我必须绝对使用模型.这很好,但就我而言,它不是静态模型,我需要动态定义字段,有时还需要更改它们.

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.

我可以重新创建模型,但我需要使用不同的名称,因为显然无法修改现有模型,也不能删除它.如果我尝试使用同名的 Ext.regModel,ExtJS 会崩溃.

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.

感谢您的帮助!

推荐答案

4.1 更新:

作为更新... 在 4.1 中,现在有一个静态方法 setFields 可用于定义模型原型字段.它在控制器的 init 方法中运行良好.

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.

当我这样做时,我想在模型类中定义一些静态字段,然后更动态地设置一些.不幸的是,新的 setFields 方法用参数替换了所有字段,但它很容易处理.

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.

此示例使用 MVC 模式,其中我的模型和商店包含在控制器的 model 数组和 store 数组中(为我提供了下面使用的方便的 getter):

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();

        });

    }

});

User 模型和 CurrentUser 存储的创建与常规非动态模型和存储完全一样只是缺少如上所示添加的动态字段.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆