简体   繁体   English

ember.js和bootstrap手风琴 - 创建视图的“余烬方式”

[英]ember.js and bootstrap accordion - the “ember way” of creating the view

My question is not how to make a bootstrap accordion work, but to try to make sure that I understand the "ember" way of doing things. 我的问题不是如何使自举手风琴工作,而是试图确保我理解“余烬”的做事方式。

I've created a working example of a bootstrap accordion here (as of 3/12/2013): http://jsfiddle.net/nrionfx/s59fA/ 我在这里创建了一个bootstrap手风琴的工作示例(截至2013年12月3日): http//jsfiddle.net/nrionfx/s59fA/

DEBUG: ——————————- 
DEBUG: Ember.VERSION : 1.0.0-rc.1
DEBUG: Handlebars.VERSION : 1.0.0-rc.3
DEBUG: jQuery.VERSION : 1.9.1 
DEBUG: ——————————-

To make the accordion collapse properly, I had to create an observer to watch the controller.content array. 为了使手风琴正确折叠,我必须创建一个观察者来观察controller.content数组。 If I did not do this, the accordion did not collapse when the elements were inserted, even if I placed the $().collapse into the didInsertElement hook. 如果我没有这样做,即使我将$()。折叠放入didInsertElement钩子,插入元素时手风琴也不会崩溃。

App.IndexView = Em.View.extend
    templateName: 'index'
    contentObserver: ( ->
        Ember.run.next this, ->
            @.$('.collapse').collapse()
      ).observes('controller.content')

Now, this works, but my question is: Is this the appropriate way of doing this under the ember framework, or should this be somewhere else, such as the didInsertElement call? 现在,这有效,但我的问题是:这是在ember框架下执行此操作的适当方式,还是应该在其他地方,例如didInsertElement调用?

---- Update ---- Final jsFiddle: http://jsfiddle.net/nrionfx/s59fA/16/ ----更新----最后的jsFiddle: http//jsfiddle.net/nrionfx/s59fA/16/

I would recommend wrapping your items in an own View. 我建议将您的物品包装在自己的视图中。 In this case, you can use the didInsertElement hook to run your logic. 在这种情况下,您可以使用didInsertElement挂钩来运行逻辑。

So your handlebars would look like: 所以你的把手看起来像:

{{#each item in controller}}
   {{view App.ItemView contextBinding="item"}}        
{{/each}}

Just move the current contents of your loop to this new template. 只需将循环的当前内容移动到此新模板即可。 The View would look somehow like this: View会看起来像这样:

App.ItemView = Em.View.extend
    templateName: 'item'
    didInsertElement: ( ->
        Ember.run.next this, ->
            @.$().collapse()
      )

I think this would be the most emberish approach. 我认为这将是最无耻的方法。 This seems better, since the logic is not rerun on every swap of the controller's content. 这似乎更好,因为逻辑不会在控制器内容的每次交换时重新运行。 As you already said the didInsertElement hook is the most appropriate place to integrate with 3rd party libraries. 正如您已经说过的,didInsertElement挂钩是与第三方库集成的最合适的位置。

Hint: It did not work, when you placed the logic in the didInsertElement hook of your IndexView, because at that time the contained collection is not rendered yet. 提示:当您将逻辑放在IndexView的didInsertElement挂钩中时,它不起作用,因为此时所包含的集合尚未呈现。

Is this the appropriate way of doing this under the ember framework, or should this be somewhere else, such as the didInsertElement call? 这是在ember框架下执行此操作的适当方法,还是应该在其他位置,例如didInsertElement调用?

For sure the right place to do this is via didInsertElement. 确保正确的地方是通过didInsertElement。 As @mavilein mentioned you can do this by creating a view for each child element. 正如@mavilein所提到的,您可以通过为每个子元素创建一个视图来完成此操作。

Another approach you might consider is using the afterRender queue. 您可能考虑的另一种方法是使用afterRender队列。 For example: 例如:

App.IndexView = Em.View.extend
    templateName: 'index'
    contentObserver: ( ->
        Ember.run.next this, ->
            @.$('.collapse').collapse()
      ).observes('controller.content')

Check out @machty's excellent post on the ember run loop or Run jquery at the end of Ember.CollectionView rendering 查看@machty在ember运行循环上的优秀帖子或在Ember.CollectionView渲染结束时运行jquery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM