wryrych
wryrych

Reputation: 1765

When to render via render and when via view?

I'm trying to figure out when to render a custom view with render and when via view. I know that rendering with render we get the full context (view, controller). But what about view? View is supposed to be for custom views and handing events.

The example below comes form ember data example

contacts.hbs

<div class="span3">
  <div class="well sidebar-nav">
    <ul class="nav nav-list">
      <li class="nav-header">All contacts</li>
      {{#each contact in controller}}
        {{view App.ContactInListView contentBinding="contact"}}
      {{/each}}
    </ul>
  </div>
</div>
<div class="span9">
  {{outlet}}
</div>

contact_in_list_view.hbs

App.ContactInListView = Em.View.extend({
  templateName: 'contact_in_list',
  tagName: 'li',
  classNameBindings: 'isActive:active',

  isActive: function() {
    return this.get('content.id') === this.get('controller.activeContactId');
  }.property('controller.activeContactId')
});

contact_in_list.hbs

{{#linkTo "contact" contact}}{{contact.fullName}}{{/linkTo}}

Couldn't just I render contact_in_list with render and pass it some controller? When should I use render and when view? What's the rule of thumb?

Upvotes: 1

Views: 96

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

Couldn't just I render contact_in_list with render and pass it some controller?

The {{render}} helper can be passed a model but not controller. Probably what you want in this case is the {{each}} helper's itemController property

{{#each contact in controller itemController="contactInList"}}
  {{view App.ContactInListView}}
{{/each}}

Have a look at API docs for Ember Handlebars.helpers

When should I use render and when view? What's the rule of thumb?

Use the {{render}} helper when you want to render a view/template in the current context using the singleton instance of the same-named controller.

Use the {{view}} helper when you want to render a view in the current context without changing to another controller. Like {{view Ember.TextField}}

Upvotes: 2

Related Questions