如何使用ExtJS5中的ViewModel绑定将Button添加到Button

时间:2015-06-13 16:27:40

标签: javascript extjs mvvm extjs5

我想在绑定后更改css类。 ExtJS5有可能吗?

我添加了评论。 小提琴: https://fiddle.sencha.com/#fiddle/olc

1 个答案:

答案 0 :(得分:1)

您的代码中有几个问题:

  1. cls无法绑定,因为没有setCls方法 - 您可以绑定 iconCls如果你想要
  2. 你不能在诸如iconCls:'{rating}'[0]之类的字符串之后添加索引 - 这在语法上是不正确的
  3. 如果您定义rating公式,则必须运行get function - get()
  4. 试试此代码

    Ext.define('Fiddle.view.FooModel', {
        extend: 'Ext.app.ViewModel',
        alias: 'viewmodel.fiddle-foo',
    
        data: {
            val: 1
        },
    
        formulas: {
            rating: function(get) {
                get();
                return 'hello-world';
            }
        }
    });
    
    Ext.define('Fiddle.view.Foo', {
        extend: 'Ext.panel.Panel',
        xtype: 'fiddle-foo',
    
        viewModel: {
            type:'fiddle-foo'
        },
    
        layout: 'hbox',
        height: 50,
        width: 250,
        items: [{
            xtype: 'button',
            text: 'rating 1',
            bind:{
             iconCls:'{rating}'
            }
        }, {
            xtype: 'button',
            text: 'rating 2',
            bind:{
             iconCls:'{rating}'
            }
        }, {
            xtype: 'button',
            text: 'rating 3',
            bind:{
             iconCls:'{rating}'
            }
        }]
    
    
    });
    
    Ext.application({
        name: 'Fiddle',
    
        launch: function() {
            new Fiddle.view.Foo({
                renderTo: document.body,
                width: 400,
                height: 400,
                title: 'Test'
            });
        }
    });