2

我有一个ContainerView,其中包含一个CollectionView。在此CollectionView在屏幕上呈现后,我需要执行一个 jquery 函数,该函数本质上是查看呈现模板的内容并执行一些显示修改。

如果我在CollectionView的didInsertElement中执行该 jquery,它可以工作,但它会为CollectionView中的每个元素执行,因为我真的只需要在最后完成一次。我该如何指定?

http://jsfiddle.net/JFqNr/(注意不会在 jsfiddle 上呈现或出于某种原因,只是为了向您展示结构)

App = Ember.Application.create();

App.FooContainerView = Ember.ContainerView.extend({
    childViews: ['elementList'],    

    elementList: Ember.CollectionView.extend({
        content: function() {
            return [
                { Title: "Dashboard", ID: "dashboard" },
                { Title: "Invoices", ID: "invoices" },
                { Title: "Expenses", ID: "expenses" },
                { Title: "People", ID: "people" },
                { Title: "Reports", ID: "reports" },
                { Title: "Settings", ID: "settings" }
            ];
        }.property(),        
       template: Ember.Handlebars.compile( '{{view.content.title}}' ),

       didInsertElement: function() {
             // perform jquery function            
       }
    }),

   didInsertElement: function() {
         // does not work if perforemed here
   }    
});

App.initialize();
​
4

1 回答 1

4

执行此操作的功能最近才添加到 master 分支,因此您需要编译自己的 Ember 版本。
您现在可以安排到一个afterRender队列中,以便在渲染所有单个视图之后运行。

App.FooContainerView = Ember.ContainerView.extend({
  // Existing code
  didInsertElement: function() {
    Ember.run.scheduleOnce('afterRender', this, function(){
      // perform jQuery function here;
    });
 }

有关代码详细信息,请参阅https://github.com/emberjs/ember.js/pull/1528

于 2012-11-24T07:50:07.567 回答