2013-12-11 28 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所知,但显然不是 –