javascript - ExtJS 5.1.1 立即在 ViewModel 中触发绑定(bind)

标签 javascript extjs mvvm data-binding extjs5

看来我对绑定(bind)时间有一些误解。我有一个简单的组合框,其值绑定(bind)到 View 模型中的某个对象。选择新值,触发更改事件,它在 setValue 方法之后触发,所以我的新值已经设置,但我的 View 模型尚未更新。我的 View 模型什么时候更新?我找到了一些有关调度程序的信息,其中说我需要运行 notify() 方法以立即将更改应用到 viewmodel,但这对我一点帮助都没有。

Ext.define('MyModel', {
    extend: 'Ext.data.Model',
    idProperty: 'foo',
    fields: [{
        name: 'bar',
        type: 'string'
    }]
});

Ext.define('MyViewModel',{
    extend: 'Ext.app.ViewModel',
    alias: 'viewmodel.my',
    data: {
        testObj: {
            foo: null,
            bar: null
        }
    },
    stores:{
        combostore: {
            model: 'MyModel',
            data: [{
                foo: '1',
                bar: 'qwerty'
            },{
                foo: '2',
                bar: 'ytrewq'
            }]
        }
    }
});

Ext.define('MyViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.my',
    onChange: function() {
        var vm = this.getViewModel();
        vm.notify();
        console.log(vm.get('testObj.foo'));//supposed to be current value
    }
});

Ext.application({
    name : 'Fiddle',

    launch : function() {
        Ext.create('Ext.container.Viewport', {
            controller: 'my',
            viewModel: {
                type: 'my'
            },
            layout : 'vbox',
            items : [
                { 
                    xtype : 'combo',
                    valueField: 'foo',
                    displayField: 'bar',
                    queryMode: 'local',
                    bind: {
                        store: '{combostore}',
                        value: '{testObj.foo}'
                    },
                    listeners:{
                        change: 'onChange'
                    }

                }
            ]
        });
    }
});

还有 fiddle :https://fiddle.sencha.com/#fiddle/r88

最佳答案

我知道这是一个迟到的回应,但我认为这个问题仍然相关。看到这个 fiddle :https://fiddle.sencha.com/#fiddle/2l6m&view/editor .

本质上,这个想法是您监听绑定(bind)变量的变化,而不是监听组合框选择的变化(这会触发绑定(bind)变量的重新评估)。

关键代码在我为 View 模型添加的构造函数中:

Ext.define('MyViewModel',{
    extend: 'Ext.app.ViewModel',
    alias: 'viewmodel.my',

    // added this to enable the binding
    constructor: function () {
        var me = this;
        me.callParent(arguments);

        me.bind('{selectedItem}', function (value) {
            console.log('combobox selected item changed (bar value): ' + (value === null ? "null": value.get('bar')));
            console.log(me.getView().getController());
        });

        me.bind('{testObj.foo}', function (value) {
            console.log('combobox value (foo value): ' + value);

            // you can access the controller
            console.log(me.getView().getController());
        });
    },
    data: {
        testObj: {
            foo: null,
            bar: null
        },
        selectedItem: null,
    },
    stores:{
        combostore: {
            model: 'MyModel',
            data: [{
                foo: '1',
                bar: 'qwerty'
            },{
                foo: '2',
                bar: 'ytrewq'
            }]
        }
    }
});

这是 View (注意绑定(bind)到选择):

Ext.application({
    name : 'Fiddle',

    launch : function() {
        Ext.create('Ext.container.Viewport', {
            controller: 'my',
            viewModel: {
                type: 'my'
            },
            layout : 'vbox',
            items : [
                {
                    xtype : 'combo',
                    valueField: 'foo',
                    displayField: 'bar',
                    queryMode: 'local',

                    bind: {
                        store: '{combostore}',
                        value: '{testObj.foo}',
                        selection: '{selectedItem}'

                    },
                    listeners:{
                        change: 'onChange'
                    }

                }
            ]
        });
    }
});

绑定(bind)选择不是必需的,但我还是将其作为另一个选项包含在内,因为有时您可能希望在绑定(bind)到列表的存储中存储额外的数据。

我希望这对其他人有帮助。

关于javascript - ExtJS 5.1.1 立即在 ViewModel 中触发绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31682264/

相关文章:

c# - 模型在 "traditional"C#/SQLite MVVM 应用程序中的作用

c# - 带有确定和取消按钮的弹出子窗口

javascript - 使用 jQuery 或其他库的 Apple Cover-flow 效果?

Javascript 动态创建 anchor 标记不起作用

javascript - ExtJs 窗口不显示

html - 如何在 100% 高度的 HTML 正文上设置 Grid ExtJS

c# - 如何在 MVVM WPF 的主窗口中加载用户控件?

javascript - 如何改进 Lighthouse 报告 (PWA) 的 "Eliminate render-blocking resources"?

javascript - 随着图像更改或动画更改文本颜色

jsp - 如何从 servlet 获取 json 对象以及如何使用该对象在 ext js 网格面板中呈现它