2012-10-29 282 views
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); 
    }, 
    ... 
}); 

回答

1

从灰烬文档上的CollectionView http://emberjs.com/api/classes/Ember.ContainerView.html

一个的CollectionView的childViews属性不应直接操作。相反,请添加,删除,替换其内容属性中的项目。这将触发对其呈现的HTML进行适当的更改。

看来,使用pushObject的直接数组操作不会触发didInsertElement事件。取而代之的是在BranchObj(第2行)初始化后触发。尝试使用content属性进行操作。