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);
},
...
});