1
這裏,RootView繪製一個初始視圖,然後創建一個BranchObj,它在其init函數中查找RootView的branchView並嘗試將另一個視圖粘貼到它上面。Emberjs代碼不按順序運行
問題是,即使第1行(應該導致控制檯記錄「1」)寫在第2行(應使控制檯記錄「2」)之前,控制檯在「1」之前記錄「2」 ,因爲BranchObj無法在RootView的branchView中找到它正在查找的視圖,所以會出現錯誤。
如何讓這兩個函數以正確的順序運行?
App.RootView = Ember.CollectionView.extend({
...
didInsertElement: function() {
//draw this branch
var branchView = App.BranchView.create();
this.set("branchView",branchView);
this.get("childViews").pushObject(branchView); //Line 1
//draw a child
App.BrachObj.create({parentView:this}); //Line 2
}
});
App.BranchView = App.RaphaelView.extend({
didInsertElement: function() {
console.log(1);
},
...
});
App.BranchObj = Ember.Object.extend({
init: function() {
console.log(2);
},
...
});