nytimes / backbone.stickit

Backbone data binding, model binding plugin. The real logic-less templates.
MIT License
1.64k stars 176 forks source link

Support falsy values in classes binding #292

Closed bmuenzenmeyer closed 9 years ago

bmuenzenmeyer commented 9 years ago

I love the new classes hash but find myself at a loss with how to evaluate a falsy value quickly.

Previous to 0.9.* I had this binding like this:

'.disableIcon': {
    attributes: [
        {
            name: 'class',
            observe: 'isDisabled',
            onGet: function(value, options) {
                return value ? 'icon-unlock' : 'icon-lock';
            }
        }
    ]
},

As far as I understand, I can only replace one side of this situation, like so:

'.disableIcon': {
    classes: {
        'icon-unlock': 'isDisabled'
    }
},

requiring me to still still evaluate the falsy path using the old attribute syntax. Would there be some way to accomplish the following?

'.disableIcon': {
    classes: {
        'icon-unlock': 'isDisabled',
        'icon-lock': !'isDisabled'
    }
},

Not a show stopper, and again, I might be going about it the wrong way. Either way, thanks for the great library!

akre54 commented 9 years ago

Yeah unfortunately with the simple key -> val syntax it doesn't leave any room for extra options. One idea is we could support {'icon-unlock': '!isDisabled'}, and run a regex against the first !, but in a perfect world I'd rather we keep away from string programming if we can, as it tends to open a whole slew of new problems. Open to other suggestions if you have any, but I'd say stick with the function for now.

bmuenzenmeyer commented 9 years ago

Okay by me - the issue was more a question to make sure I didn't miss something obvious.

Thanks for the quick response!

victorwpbastos commented 9 years ago

+1