0

我在 emberjs.com http://emberjs.com/documentation/的“文档”页面上输入了以下代码

但是,它并没有显示出我期望的结果。这是为什么?

App.wife = Ember.Object.create({
    householdIncome: 80000
});

App.husband = Ember.Object.create({
    householdIncomeBinding: 'App.wife.householdIncome'
});

console.log(App.husband.get('householdIncome')); //it shows 80000

App.husband.set('householdIncome', 90000);

**console.log(App.wife.get('householdIncome')); // it shows 80000 not 90000**
console.log(App.husband.get('householdIncome')); // this shows 90000

正如 ember.js 上的示例代码所说,当我输入 console.log(App.wife.get('householdIncome')); 时,我期望得到 90000。

有谁知道出了什么问题?请帮帮我。

谢谢!!

4

1 回答 1

1

From that same documentation:

Note that bindings don't update immediately. Ember waits until all of your application code has finished running before synchronizing changes, so you can change a bound property as many times as you'd like without worrying about the overhead of syncing bindings when values are transient.

You can wrap your console.log statements with Ember.Run.next to make sure the binding updates are applied before they run.

App.husband.set('householdIncome', 90000);

Ember.run.next(function() {
  console.log("her income: " + App.wife.get('householdIncome')); // it shows 90000
  console.log("his income: " + App.husband.get('householdIncome')); // it also shows 90000
});​
于 2012-12-26T03:06:46.067 回答