2013-09-10 88 views
0

我正在將我的代碼庫從EmberJS-1.0-rc1升級到EmberJS-1.0。EmberJS綁定不同步?

我的許多綁定似乎不再一致同步。語義必須改變,但我無法找到如何,或者我應該做什麼!

var Widg = Ember.Object.extend({ 
    init: function() { 
     this._super(); 

     console.log("aliasToValue is: ", this.get('aliasToValue')); 
     if (this.get('aliasToValue') != this.get('value')) { 
      throw "This exception doesn't get hit..."; 
     } 

     console.log("Setting value to 'hello world'"); 
     this.set('value', "hello world"); 

     // I would expect: 
     // this.get('value') == this.get('aliasToValue') 
     // but that doesn't seem to work anymore.... 

     console.log("After settting, aliasToValue is: ", this.get('aliasToValue')); 
     if (this.get('aliasToValue') != this.get('value')) { 
      throw "Ugh, after setting, they don't match"; 
     } 
    }, 
    value: "initial value", 
    aliasToValueBinding: "value" 
}); 

Widg.create(); // EXCEPTION: "Ugh, after setting, they don't match" 
+0

的jsfiddle演示該問題:http://jsfiddle.net/kxhUw/1/ – Seth

+0

的[Ember.bind不同步留]可能重複(HTTP ://stackoverflow.com/questions/19121850/ember-bind-does-not-stay-in-sync) –

回答

0

當添加propertyABinding: propertyB,基本上爲餘燼和propertyApropertyB安排在sync隊列中的同步化(的propertyA值設置爲propertyB,反之亦然)創建一個觀察者。由於該同步化計劃,它將在runloop結束時執行。因此,aliasToValue不改:

this.set('value', "hello world"); 
this.get('value'); // return "hello world" 
this.get('aliasToValue'); // return "initial value" 

您可以進行同步隊列被刷新編程調用Ember.run.sync()。因此,這將工作:

this.set('value', "hello world"); 
this.get('value'); // return "hello world" 
Ember.run.sync(); // don't wait for the end of runloop, and make the sync here 
this.get('aliasToValue'); // return "hello world" 

但是對於您的示例中的這項工作,您需要進行一些更改。在你的情況下,你正在設置init函數的屬性。但是在init函數中,observes沒有被觸發。所以,不會安排同步。您可以使用on('init')執行對象中的變化時,它被初始化:

... 
didInit: function() { 
    // now the observes will trigger when a property change 
}.on('init') 
... 

您的代碼更新將是以下幾點:

//the application 
App = Ember.Application.create(); 

App.MyObject = Ember.Object.extend({ 
    didInit: function() { 

     console.log("aliasToValue is: ", this.get('aliasToValue')); 
     if (this.get('aliasToValue') != this.get('value')) { 
      alert("This exception doesn't get hit..."); 
     } 

     console.log("Setting value to 'hello world'"); 
     this.set('value', "hello world"); 

     // I would expect: 
     // this.get('value') == this.get('aliasToValue') 
     // but that doesn't seem to work anymore.... 

     console.log("After settting, aliasToValue is: ", this.get('aliasToValue')); 
     Ember.run.sync(); 
     if (this.get('aliasToValue') != this.get('value')) { 
      alert("Ugh, after setting, they don't match"); 
     } 
    }.on('init'), 
    value: "initial value", 
    aliasToValueBinding: "value" 
}); 

App.MyObject.create(); // EXCEPTION: "Ugh, after setting, they don't match" 

而且的jsfiddle http://jsfiddle.net/marciojunior/Bk7L9/

當然Ember.run.sync()中只是在罕見的情況下使用,刪除它會使您的模板和屬性更新,但runloop完成時。我用它來立即看到綁定屬性更新。

有關runloop進一步信息,請參閱此答案What is Ember RunLoop and how does it work?