0

我的 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 动态中首次显示的相同内容部分。

我应该做什么或不应该做什么?

4

2 回答 2

1

因此,对于将来碰巧遇到此问题的任何人,如果您使用 collectionView 在动态段中呈现元素而不是每个语句的句柄,请务必像这样设置“contentBinding”属性

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

否则,当您输入动态分段的不同分段时,您的内容将保持静态。

将此标记为正确答案,如果有帮助,请给它 +1

于 2013-12-13T04:30:49.327 回答
0

元素被缓存,所以 didInsertElement 不会重新触发。在 emberjs.jsbin.com 上设置 jsbin 以获得更多帮助。

于 2013-12-12T03:38:48.897 回答