【问题标题】:Extjs4 How to change a Model dynamically? [closed]Extjs4 如何动态更改模型? [关闭]
【发布时间】:2012-06-16 21:59:04
【问题描述】:

我使用 Extjs4 和 MVC。我想动态更改模型的字段... 类似于添加可变数量的字段...有什么建议吗?

【问题讨论】:

  • 你到底想做什么?你能更具体一点吗?还有你用的是什么版本的 ExtJs?
  • Extjs4,例如我有一个有 3 个字段的模型...我想用其他六个字段扩展它...
  • 这个问题不应该因为不是一个真正的问题而被关闭!这是一个完全合理的问题。不要仅仅因为你不明白被问到什么而投票关闭。也就是说,它是重复的。

标签: extjs model


【解决方案1】:

您可以使用 model.setFields(fieldsArray) 函数,在 API 中显示为 here。此方法将模型上的所有现有字段替换为您在参数中包含的任何新字段。没有静态的getFields 方法来捕获现有字段以免覆盖它们,但使用model.prototype.fields 获取它们很容易。

我最近这样做是为了在加载用户之前将动态权限设置字段附加到“用户”模型。这是一个例子:

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

    models: [
        'User',
    ],

    stores: [
        'CurrentUser', // <-- this is not autoLoad: true
        'PermissionRef', // <-- this is autoLoad: true
    ],

    views: ['MainPanel'],

    init: function() {
        var me = this;

        // when the PermissionRef store loads 
        // use the data to update the user model
        me.getPermissionRefStore().on('load', function(store, records) {
            var userModel = me.getUserModel(),
                fields = userModel.prototype.fields.getRange();
                // ^^^ this prototype function gets the original fields 
                // defined in myApp.model.User

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

            // update the user model with ALL the fields
            userModel.setFields(fields);

            // NOW load the current user with the permission data
            // (defined in a Java session attribute for me)
            me.getCurrentUserStore().load();

        });

    }
});

【讨论】:

  • 我回答后才注意到这是this one的重复。我在那里给出了完全相同的答案......
猜你喜欢
  • 1970-01-01
  • 2015-09-22
  • 2021-03-27
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 2020-06-10
相关资源
最近更新 更多