javascript - 如何在 ExtJS5 中使用 ViewModel 绑定(bind)将 Cls 添加到 Button

标签 javascript extjs mvvm extjs5

我想在绑定(bind)后更改 css 类。在 ExtJS5 中可以吗?

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

最佳答案

您的代码中有几个问题:

  1. cls 无法绑定(bind),因为没有 setCls 方法 - 您可以绑定(bind) 如果需要的话请关闭图标
  2. 您无法在字符串后添加索引,例如 iconCls:'{ rating}'[0] - 这在语法上是不正确的
  3. 如果您定义这样的 rating公式,则必须运行get函数 - get()

试试这个代码

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'
        });
    }
});

关于javascript - 如何在 ExtJS5 中使用 ViewModel 绑定(bind)将 Cls 添加到 Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30820939/

相关文章:

javascript - 使用 Jasmine 测试 Java Script 应用程序

json - ExtJs,如何使用 POST 方法在 store.load() 上发送 JSON

mvvm - 如何从 ViewModel 调用 TapGestureRecognizer

c# - 为什么转换器没有被调用

javascript - 如何在节点后打印另一个 div 元素

javascript - 使用 Selenium 检查是否选择了网页上的选项卡

javascript - ExtJS::如何过滤网格中的行,但不过滤存储中的行

c# - 单击 WPF 中的按钮显示用户控件 (WPF) 查看页面

javascript - 将变量传递给 JavaScript 函数

javascript - $ 未定义,但 jQuery 在 WordPress 中已定义