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

余生颓废 提交于 2019-12-22 01:28:14

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/15258054/whats-the-difference-between-ember-computed-alias-and-an-ember-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!