EXT.js学习(数据绑定)

任何组件配置都可以使用绑定,只要它有一个 setter 方法。在这种情况下,我们将记录的 firstName 字段绑定到我们的textfield的值(默认为textfield绑定)。

Ext.Viewport.add({
    xtype: 'tabpanel',
    items: [{
        title: 'Employee Directory',
        xtype: 'grid',
        iconCls: 'x-fa fa-users',
        listeners: {
            itemtap: function(view, index, item, record) {
                Ext.Viewport.add({
                    xtype: 'formpanel',
                    title: 'Update Record',
                    width: 300,
                    floating: true,
                    centered: true,
                    modal: true,

                    record: record,
                    viewModel : {
                        data: {
                            employee: record
                        }
                    },
                    items: [{
                        xtype: 'textfield',
                        name: 'firstname',
                        label: 'First Name',
                        bind: '{employee.firstName}'

                    }, {
                        xtype: 'toolbar',
                        docked: 'bottom',
                        items: ['->', {
                            xtype: 'button',
                            text: 'Submit',
                            iconCls: 'x-fa fa-check',
                            handler: function() {
                                this.up('formpanel').destroy();
                            }
                        }, {
                            xtype: 'button',
                            text: 'Cancel',
                            iconCls: 'x-fa fa-close',
                            handler: function() {
                                this.up('formpanel').destroy();
                            }
                        }]
                    }]
                });
            }
        },

        store: {
            data: [{
                "firstName": "Jean",
                "lastName": "Grey",
                "officeLocation": "Lawrence, KS",
                "phoneNumber": "(372) 792-6728"
            }, {
                "firstName": "Phillip",
                "lastName": "Fry",
                "officeLocation": "Lawrence, KS",
                "phoneNumber": "(318) 224-8644"
            }, {
                "firstName": "Peter",
                "lastName": "Quill",
                "officeLocation": "Redwood City, CA",
                "phoneNumber": "(718) 480-8560"
            }]
        },
        columns: [{
            text: 'First Name',
            dataIndex: 'firstName',
            flex: 1
        }, {
            text: 'Last Name',
            dataIndex: 'lastName',
            flex: 1
        }, {
            text: 'Phone Number',
            dataIndex: 'phoneNumber',
            flex: 1
        }]
    }, {
        title: 'About Sencha',
        iconCls: 'x-fa fa-info-circle'
    }]
});

所有这些绑定是可能的,凭借我们的 ViewModel

ViewModel是一个管理数据对象的类。然后它允许那些对这些数据感兴趣的人绑定到它,并在它改变时做出反应。ViewModel由定义它的视图所有。因为ViewModel与视图相关联,所以它的子视图只是“继承”其父ViewModel的数据。

在上面的示例中,我们的textfield的值绑定到从我们选择的记录传递的值。如果更改表单字段中的值,它也将更改所选记录中的值,并且更改将反映在行中。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 译自《Editing Models and Validation》 编辑模型和验证 作为开发人员,TornadoF...
    公子小水阅读 1,910评论 0 3
  • 1、概述 Databinding 是一种框架,MVVM是一种模式,两者的概念是不一样的。我的理解DataBindi...
    Kelin阅读 76,876评论 68 521
  • 搜狐 有一种人虽然活在这个世界,却总是保持着一种无可诉说的警觉性与失落感,他很难找到内心安定的港湾,对于这个...
    金牧云阅读 275评论 0 0
  • 怎么说呢?如果他是处,你最好在一个合适的时机和他说清楚,看他的反应,如果比较剧烈,给他一点时间,同时也说清楚,过去...
    加油冲哇阅读 587评论 0 0