How can addCls to Button with ViewModel binding in ExtJS5

988 views Asked by At

I want to change css class after binding. Is it possible in ExtJS5?

I added to comments. fiddle: https://fiddle.sencha.com/#fiddle/olc

1

There are 1 answers

0
Saki On BEST ANSWER

You have several problems in your code:

  1. cls cannot be bound as there is no setCls method - you can bind iconCls if you want
  2. you cannot add index after string such as iconCls:'{rating}'[0] - this is syntactically incorrect
  3. if you define rating formula like that you have to run get function - get()

Try this code

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