我的 EmberApp 在资源下加载了动态段。动态段对应的控制器是 ArrayController。正如你们大多数人所知道的,简单的#each 循环遍历模板层中的控制器内容或模型将按应有的方式呈现内容。就我而言,它确实如此,除了我需要使用“CollectionView”,它又定义了一组子视图。原因是我需要根据用户操作执行添加/删除这些视图。这么短的故事,我的代码如下所示:
<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>
使用集合视图的问题似乎是Ember 正在缓存这些视图,本质上,当我转换到一个新的动态段时,比如从 /1 到 /2,然后 /2 仍然呈现在 /1 动态中首次显示的相同内容部分。
我应该做什么或不应该做什么?