Ember CollectionView包含具有不同模板的视图

Ale*_*yan 2 ember.js

鉴于我有这个代码:

Ember.App.View = Ember.View.extend({
    templateName: "templateA"
});
Ember.App.View = Ember.View.extend({
    templateName: "templateB"
});
Ember.App.ViewC = Ember.CollectionView.extend({
    itemViewClass: "Ember.View",
    contentBinding: "SomeController"
});
Run Code Online (Sandbox Code Playgroud)

有没有办法将不同模板的视图添加到CollectionView?

pan*_*atz 6

您可以进行templateName的的itemViewClass一个计算的属性,看http://jsfiddle.net/pangratz666/vGHcD/:

App.MyView = Ember.View.extend({
    templateName: function() {
        var templateName = Ember.getPath(this, 'content.label');
        // return default template if there is no such template 
        return (!Ember.TEMPLATES[templateName]) ? 'default' : templateName;
    }.property('content.label').cacheable(),

    // rerender the view if the template name changes
    _templateNameChanged: function() {
        this.rerender();
    }.observes('templateName')
});

App.CollectionView = Ember.CollectionView.extend({
    itemViewClass: 'App.MyView'
});
Run Code Online (Sandbox Code Playgroud)

另请参阅相关问题:使用Ember.js按模型类型/对象值选择视图模板