2013-12-11 23 views
0

我的EmberApp在資源下加載了動態細分。動態段的相應控制器是一個ArrayController。正如大多數人所知道的那樣,簡單的#each循環遍歷模板層中的控制器內容或模型將會呈現內容。在我的情況下,它的確如此,除了我需要使用一個'CollectionView'來反過來定義一個子視圖集合。原因是我需要根據用戶操作執行添加/刪除這些視圖。這麼短的故事,我的代碼看起來是這樣的:在動態細分中使用EmberJS集合視圖似乎緩存視圖

<script type="text/x-handlebars" data-template-name="dynamicSegment"> 
    {{view App.CollectionView}}                                    
</script> 
App.CollectionView = Ember.CollectionView.extend({ 
    tagName: "ul" // does not have a template corresponding to it, but is generated automagically ? 
    itemViewClass: Ember.ChildView.extend(), 
    didInsertElement:function(){ 
     this.set('content', this.get('controller').get('content')); // notice that I am setting up the content for this view in the didInsertElement method, this could be wrong, but I know no other way 
    }, 
}); 

App.ChildView = Ember.View.extend({ 
    templateName:"_childView" // has a template defined within handlebars 
}); 
<script type="text/javascript" data-template-name="_childView"> 
    <div>{{view.content.modelProperty1}}</div> <!-- notice how I have to actually use view.content.modelProperty1 instead of simply modelProperty1 
</script> 

以上,你可以講的是呈現在EmberJS陣列信息略微非常規的方式,在通常人會簡單地說:

{{#each}} 
    {{view App.ChildView}} 
{{/each}} 

和該ChildView內,

<div>{{modelProperty1}}</div> 

使用收集意見的問題似乎是灰燼是緩存這些意見,實質上,W如果我過渡到一個新的動態片段,比如從/ 1到/ 2,那麼/ 2仍會呈現最先顯示在/ 1動態片段中的相同內容。

我應該做什麼或者我應該怎麼做?

回答

1

因此,對於任何人誰發生在未來在此跌倒,如果你使用的的CollectionView呈現在動態段,而不是一個車把每個語句的元素,一定要設置「contentBinding」屬性這樣

App.MyCollectionView = Ember.CollectionView.extend({ 
     contentBinding:"controller" // this is the magic property 
}); 

否則,當您輸入動態細分受衆羣的不同細分時,您的內容會保持不變。

將此標記爲正確答案,或者給它一個+1,如果這是有幫助的

0

元素被緩存,所以didEsertElement不會重新啓動。在emberjs.jsbin.com上設置一個jsbin以獲取更多幫助。

+0

不,我想通了這一點,所有我所要做的就是 contentBinding:在我的CollectionView「控制器」這樣每次一動態片段被輸入,內容的底層內容被更新。你會認爲像Ember這樣的東西會自動被Ember所知,但顯然不是 –