Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Ember.computed.alias and an Ember.binding?

Tags:

ember.js

In Ember, defining a property as a computed alias to another property (or another object's property) using Ember.computed.alias('otherProperty') seems to have basically the same result as defining it as a binding to that property using propertyNameBinding: 'otherProperty'.

I've looked at the source and the documentation but I can't find any reason why one would be preferred over the other. Obviously templates use bindings, which is fine, but for properties in e.g. controllers, or for referencing a controller's property on a view, is there a difference?

like image 514
Nick Ragaz Avatar asked Mar 06 '13 21:03

Nick Ragaz


1 Answers

An alias can be overwritten on extend(), completely eliminating the relationship with the other key.

A brief example: I have a class of time-related functions and classes, and the classes have both a unit property (for Day, Minute, etc) and a precision property. Normally these are functionally identical. In one or two classes they diverge. Ember.computed.alias allows precision to alias to unit for most cases, but to be overridden with its own value as needed.

Note that create() will use the alias setter, instead of overriding.

Also, setting an alias with a null object in its path can blow up, while a binding will simply not sync if the path doesn't lead anywhere.

Please see morgoth's comment below per soft-deprecation of *Binding syntax

like image 114
Christopher Swasey Avatar answered Oct 02 '22 06:10

Christopher Swasey