Dynamically choosing a view at runtime with Ember + Handlebars

后端 未结 4 827
眼角桃花
眼角桃花 2021-02-04 20:10

I am using Ember, Ember Data, and Handlebars to display a timeline with a number of different types of models. My current implementation, though functioning properly, seems lik

4条回答
  •  迷失自我
    2021-02-04 20:48

    I solved this by establishing my own convention using a mixin. A model corresponds to a view with a similar name. For example, an App.Design model instance corresponds to the view App.DesignView.

    App.ViewTypeConvention = Ember.Mixin.create({
      viewType: function() {
        return Em.getPath(this.get('constructor') + 'View');
      }.property().cacheable()
    });
    

    I mix this into my models...

    App.Design.reopen(App.ViewTypeConvention);
    App.Order.reopen(App.ViewTypeConvention);
    

    ...and iterate over a mixed collection like this:

    {{#each content}}
      {{view item.viewType tagName="li" contentBinding="this"}}
    {{/each}}
    

    This way, I avoid defining the convention explicitly in my models. Thanks to Gordon, I realized that the view could by specified using a property on an object. I would still really like to hear about the 'right' way to solve this problem.

提交回复
热议问题