2

Is there a logical not for handlebars bindings with Ember.js?

Suppose I have a ember view that I want to bind to a value

{{Ember.Button disabledBinding="view.controller.some_value"}}

I only want the button to be disabled if some_value is false. The code above makes it disabled if some_value is true.

One approach to fixing this would be to have a complementary computed value on the controller. excuse my coffeescript

opposite_some_value: (->
    if @get('some_value') == true
        return false
    else
        return true
).property 'some_value'

But this seems clunky.

wmarbut
  • 4,595
  • 7
  • 42
  • 72
  • Handlebars supports the logical not in `if` statements through the complementary `unless` statement. http://handlebarsjs.com/ – wmarbut Aug 06 '12 at 18:57

1 Answers1

9

Creating a property with the inverted value is the way to go. You can use binding helper for this: oppositeValueBinding: Ember.Binding.not('some_value').

Also note the Ember.Button is deprecated and you should use the {{action}} helper instead.

UPDATE

In newer versions of Ember.js, it's oppositeValue: Ember.computed.not('some_value').

pangratz
  • 15,875
  • 7
  • 50
  • 75
  • 1
    thats not a computed property. thats a binding with a tranform. – hvgotcodes Jul 26 '12 at 16:40
  • Bummer... Wish it could just be handled within handlebars. Thanks! – wmarbut Jul 26 '12 at 16:59
  • 1
    You shouldn't push such logic into the template. This is should be handled in the View/Controller. – pangratz Jul 26 '12 at 18:38
  • 1
    @pangratz I generally agree that logic shouldn't be in the template, but taking an inverse value hardly counts. I don't see this being any more complex than an `if` statement in a template. – wmarbut Jul 30 '12 at 20:50
  • 1
    The way to bind inverted values is now as follows: `oppositeValueBinding: Ember.computed.not('some_value')`. See https://github.com/emberjs/ember.js/blob/master/packages/ember-metal/lib/computed.js – Panagiotis Panagi Aug 04 '12 at 09:49