Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js set model data for {{render}} helper

I'm trying to build a view that is going to display multiple models and aggregated data.

enter image description here

After doing some reading in the docs, the {{render}} helper is probably the right way to build a view like this. For setting the model normally I'd just set up a Route in which I pass the needed model data:

App.BuildingsRoute = Ember.Route.extend({
    model: function() {
         return this.store.find('buildings', '1');
    }
});

But if I include a template via the {{render}} helper, the route is not called. I'd like to know how I can pass the different models for each {{render}} helper independently form each other, including first filtering the model?

like image 672
wowpatrick Avatar asked Mar 09 '14 11:03

wowpatrick


1 Answers

The following may be relevant:

In your BuildingsRoute you can have all of the required logic to return all relevant models e.g.:

App.BuildingsRoute = Ember.Route.extend({
    model: function() {
         var self = this;
         return new Em.RSVP.Promise(function (resolve, reject) {
                new Em.RSVP.hash({
                    building: self.store.find('building', params.buildingId),
                    clients: self.store.find('client'),
                    products: self.store.find('product')
                }).then(function (results) {
                   resolve({
                       building: results.building,
                       prods: results.broducts,
                       clients: results.clients,
                       foo: "bar"                            
                   });
                });
          }
  });

See promises for more info on how to structure and chain these.

Then in your view you can access these models as follows:

{{render 'products' products}}

or

{{#each product in products}}
   {{render 'product' product}}
{{/each}}
like image 181
TrevTheDev Avatar answered Sep 24 '22 08:09

TrevTheDev