Bindings are one of the killer features of Ember.js. When you use the framework your are likely to use this good stuff probably via adding a Binding to your property name, like valueBinding: 'App.controller.value'. There are some enhanced ways of using bindings which are covered in this blog post. I’ll show how you can create bindings which transform values, convert a value to a boolean and how chained bindings work.
Hey! This article references a pre-release version of Ember.js. Now that Ember.js has reached a 1.0 API the code samples below are no longer correct and the expressed opinions may no longer be accurate.
The stuff which handles bindings is defined in binding.js in the ember-metal package whereas the corresponding tests are located here. The source and the tests are definitely worth a look as they’ll give you in insight how bindings are implemented and the tests show how they can be used.
The magic where a property ending with Binding is bound to the specified path happens in packages/ember-runtime/lib/ext/mixin.js. What it basically does is to check if the key ends with ‘Binding’ and if so, it creates a new Ember.Binding object if the value is not already one. So the following two statements are the same but the first approach is much more concise and elegant.
Specifying the path to a property via valueBinding: 'App.controller.value' is limited though. It’s not possible to specifiy that an uppercase String version of the bound value shall be used. Transforming the length of an array to a boolean hasMoreThanTwoEntries is also not possible. This is where the Ember.Binding object enters the game. It offers useful methods for enhanced bindings which are covered in the next sections.
If you need to bind to a property and only want to propagate changes in one direction you can use the #oneWay binding. Changes are only forwarded from the from side to the to side, which means that if the to side is changed directly, the from side may have a different value (as you can see in line #7 in the following example).
If you want to assure that your bound value only has a single item and is not an array, use the #single binding. You can specify a placeholder which is returned when the array has more than one entry.
You can use any object as placeholder, so you’re not restricted to use a String.
#bool() converts the from value to a boolean, where the transformation returns true for every value except null, undefined, an empty string or an empty array. #not() has the same rules as the #bool() binding, only that it inverts it. #isNull() transforms the value to true if the source is either null or undefined.
By using these bindings you can combine two paths logically by and respectively or. If one of the sources changes, the bound value is recalculated. This binding only works in one way.
For the and transformation the returned value is the result of Ember.getPath('pathA') && Ember.getPath('pathB') where for the or transformation the calculation is Ember.getPath('pathA') || Ember.getPath('pathB').
If none of the above transformations fits your needs, you can simply write your own transformation function. Simply add a transformation function to the transform binding helper. If you want to support bi-directional bindings, pass a hash with to and from properties.
It’s possible to chain bindings and hereby create even more powerful constructs. So you could create an oneWay binding which transforms to the uppercase version of a string.
varuppercase=function(value){return(Ember.typeOf(value)==='string')?value.toUpperCase():value;};varobj=Ember.Object.create({value:'',uppercaseValueBinding:Ember.Binding.from('value').transform(uppercase),oneWayUppercaseValueBinding:Ember.Binding.oneWay('value').transform(uppercase)});obj.set('value','hello');// value = 'hello' ; oneWayUppercaseValue = 'HELLO' ; uppercaseValue = 'HELLO'obj.set('oneWayUppercaseValue','world');// value = 'hello' ; oneWayUppercaseValue = 'world' ; uppercaseValue = 'HELLO'obj.set('uppercaseValue','This Works Too');// value = 'This Works Too' ; oneWayUppercaseValue = 'THIS WORKS TOO' ; uppercaseValue = 'This Works Too'obj.set('value','buster');// value = 'buster' ; oneWayUppercaseValue = 'BUSTER' ; uppercaseValue = 'BUSTER'
Manually create bindings
It’s also possible to define bindings outside of an object definition, although as stated in the docs you should almost always use the higher level APIs to create bindings.
To create a new binding instance simply call var binding = Ember.Binding.from('App.controller.value').to('myValue'). This describes a binding from the value property of App.controller to a myValue property.
To actually connect the binding to a specific object just invoke #connect() and to disconnect invoke the #disconnect() method. UPDATE: as Peter stated in the comments, it’s important to say if you manually create bindings via #connect you have to make sure to #disconnect them as soon as you don’t need them anymore. Otherwise you’ll run into memory leaks. If you use the ***Binding syntax you’re fine since Ember.js handles this for you.
There are shortcuts available: Ember.bind() and Ember.oneWay().
But again, always use the elegant approach via propertyBinding: 'App.controller.value' respectively propertyBinding: Ember.Binding.oneWay('App.controller.value') …
Summary
Bindings in Ember.js are a very mighty concept. By using transformations and custom transformations they are even mightier. I hope this post gave you some insight in bindings and how you can use them in your applications.