2013-03-12 18 views
4

我的問題不是如何製作自舉手風琴,而是試圖確保我理解「做事」的「餘燼」方式。ember.js和自舉手風琴 - 創建視圖的「燼路」

我創建了一個自舉手風琴的工作示例這裏(截至2013年3月12日): 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: ——————————- 

爲了使手風琴崩潰正確,我不得不創建一個觀察者觀看controller.content數組。如果我沒有這樣做,即使將$()。collapse合併到didInsertElement鉤子中,插入元素時手風琴也不會崩潰。

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

現在,這個工作,但我的問題是: 這是餘燼框架下這樣做的適當的方式,還是應該這是別的地方,比如didInsertElement電話嗎?

---- ----更新最終 的jsfiddle: http://jsfiddle.net/nrionfx/s59fA/16/

+0

我已經更新了誰碰到這個跌倒的人搗鼓:http://jsfiddle.net/nrionfx/s59fA/ 7/ – nrion 2013-03-12 15:32:12

+0

另外一個註釋 - 我的問題(以及下面的答案)並沒有問到摺疊所有的父手風琴。 – nrion 2013-03-12 16:15:14

+0

我已經更新了更多的小提琴插圖@MikeGrassotti解決方案,它確實可以完全摺疊父手風琴。 http://jsfiddle.net/nrionfx/s59fA/12/ – nrion 2013-03-12 18:13:34

回答

2

我會建議在自己的包裹查看您的項目。在這種情況下,您可以使用didInsertElement鉤子來運行您的邏輯。

所以,你的車把看起來像:

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

只動循環的當前內容到這個新模板。該視圖看起來在某種程度上是這樣的:

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

我認爲這將是最emberish方法。這似乎更好,因爲邏輯不會重新運行在控制器內容的每次交換上。正如您所說的,didInsertElement鉤子是與第三方庫集成的最合適的地方。

提示:當您將邏輯放置在IndexView的didInsertElement鉤子中時,它不工作,因爲那時包含的集合尚未呈現。

2

這是在ember框架下這樣做的適當方式,還是應該在別的地方,比如didInsertElement調用?

肯定要做到這一點的地方是通過didInsertElement。正如@mavilein提到的,你可以通過爲每個子元素創建一個視圖來實現這一點。

您可能會考慮的另一種方法是使用afterRender隊列。例如:

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

退房@ machty的優秀崗位上ember run loopRun jquery at the end of Ember.CollectionView rendering

+0

我把這個當成了鏡頭 - 這似乎是你所指的? 'didInsertElement: - > Ember.run.scheduleOnce'afterRender',this,() - > @。$('。collapse')。collapse({parent:'#accordion2'})' – nrion 2013-03-12 17:32:11

+0

Thanks for the links as好 - 我沒有遇到他們中的任何一個,他們非常有幫助 – nrion 2013-03-12 17:33:42

+0

沒問題 - 很高興幫助。 – 2013-03-12 19:05:17