Select view template by model type/object value using Ember.js

You can make templateName a property and then work out what template to use based on the content.

For example, this uses instanceof to set a template based on the type of object:

App.ItemView = Ember.View.extend({
    templateName: function() {
        if (this.get("content") instanceof App.Foo) {
            return "foo-item";
        } else {
            return "bar-item";
        }
    }.property().cacheable()
});

Here’s a fiddle with a working example of the above: http://jsfiddle.net/rlivsey/QWR6V/

Leave a Comment