0

I'm trying to build a view to display a user card, from their id. So ideally my calling handlebars would look something like:

<p>{{view App.UserThumb authorId}}
{{comment}}</p>

And then in my UserThumb view, I'd like to be able to load a model, in some sort of setup method or model function, sort of how I'm using controllers:

App.UserThumb = Ember.View.extend({
  model: function(view, authorId) {
    User.find(authorId, function(user) { view.set('content', user); } );
  }
}

Can anyone help me understand the 'right' or at least a workable way to do this? Worst case I can go and create the objects first, but I'd like to just keep the id around for a bit first, unless that is just totally at odds with the philosphy of Ember.

Bennidhamma
  • 730
  • 1
  • 7
  • 17

2 Answers2

1

This should do

{{#each id in listOfIds}}
  {{view App.UserThumb idBinding="id"}}
{{/each}}

App.UserThumb = Ember.View.extend({
  didInsertElement: function() {
    var authorId = this.get('id');
    User.find(authorId, function(user) { view.set('content', user); } );
  }
}

Only after the view is inserted the didInsertElement hook gets executed which gets the required user

Mudassir Ali
  • 7,913
  • 4
  • 32
  • 60
0

The model hook you re using in your View is only available inside a Route. The model hook can be used to setup the model for a controller.

See here the docs for more info on that.

In the spirit of DRY (Dont Repeat Yourself) I'd like to link to this great SO answer that will help setup a functional application, the ember way.

Hope it helps

Community
  • 1
  • 1
intuitivepixel
  • 23,302
  • 3
  • 57
  • 51
  • I've already read that general overview of ember architecture, and it does not answer my question. I'm looking for a way to load item views inside of an each block in my template, where the array is a list of ids, and I want to lazily load the associated data objects after the initial template is rendered. – Bennidhamma May 19 '13 at 01:37