Coding With Fun
Home Docker Django Node.js Articles FAQ

Ember data binding


May 09, 2021 Ember.js Reference documents



Like other frameworks, Ember its own way of binding data, and can use binding on any object. However, data binding is mostly used in the Ember and it is easier for developers to use compute properties.

Two-way binding

  1. // 双向绑定
  2. Wife = Ember.Object.extend({
  3. householdIncome: 800
  4. });
  5. var wife = Wife.create();
  6. Hasband = Ember.Object.extend({
  7. // 使用 alias方法实现绑定
  8. householdIncome: Ember.computed.alias('wife.householdIncome')
  9. });
  10. hasband = Hasband.create({
  11. wife: wife
  12. });
  13. console.log('householdIncome = ' + hasband.get('householdIncome')); // output > 800
  14. // 可以双向设置值
  15. // 在wife方设置值
  16. wife.set('householdIncome', 1000);
  17. console.log('householdIncome = ' + hasband.get('householdIncome')); // output > 1000
  18. // 在hasband方设置值
  19. hasband.set('householdIncome', 10);
  20. console.log('wife householdIncome = ' + wife.get('householdIncome'));

Ember data binding

It is important to note that the binding does not immediately update the corresponding value, Ember waits until the program code is finished running and before the synchronization changes, so you can change the value of the calculated property several times. Because binding is short,there is no need to worry about overhead.

One-way binding

One-way binding only propagates changes in one direction. One-way binding is performance-optimized relative to two-way binding, and for two-way binding, if you just set the association in one direction, it's actually a one-way binding.

  1. var user = Ember.Object.create({
  2. fullName: 'Kara Gates'
  3. });
  4. UserComponent = Ember.Component.extend({
  5. userName: Ember.computed.oneWay('user.fullName')
  6. });
  7. userComponent = UserComponent.create({
  8. user: user
  9. });
  10. console.log('fullName = ' + user.get('fullName'));
  11. // 从user可以设置
  12. user.set('fullName', "krang Gates");
  13. console.log('component>> ' + userComponent.get('userName'));
  14. // UserComponent 设置值,user并不能获取,因为是单向的绑定
  15. userComponent.set('fullName', "ubuntuvim");
  16. console.log('user >>> ' + user.get('fullName'));

Ember data binding

There are not many knowledge points about data binding, relatively not the focus, after all, the association between objects is less, the simpler the better. M ore correlations are difficult to maintain.
The full code of the blog post is placed in Github (the blog post has been modified several times, and the code on the blog post may be different from the github code, but the impact is small!). I f you think the blog post is a bit of a github project for you, give me star Yours is definitely the biggest motivation for me!!