Reputation: 1321
I have several (simple) models like languages, departments etc. They only contains name and id properties (columns). I want to make a controller and view, which controls the CRUD functions. How should i approach this problem to have one controller for several models?
Is it possible to load models from a routing variable?
pseudo code
somecontroller/modelname
App.IndexRoute = Ember.Route.extend({
model: function(modelname) {
return this.get('store').find(modelname);
}
});
Upvotes: 0
Views: 75
Reputation: 681
Here is two ways that you can do it to get two models on a route
/*
* Use the model hook to return model, then
* setController to set another model
*/
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('languages');
},
setupController: function(controller, model) {
this._super(controller, model);
controller.set('department', this.store.findAll('department'));
}
});
/*
* Can return a Hash of promises from the model hook
* and then use those as your models
*/
App.RsvphashRoute = Ember.Route.extend({
model: function() {
return Ember.RSVP.hash({
langs: this.store.find('languages1'),
dept: this.store.find('department1')
});
}
});
Here is jsbin of them in action. Hope it helps:
http://emberjs.jsbin.com/basoneno/1/edit
Upvotes: 0
Reputation: 8389
You can load multiple models from the model hook and assign them to controller properties. e.g.
App.IndexRoute = Ember.Route.extend({
model: function(modelname) {
var store = this.get('store');
return Ember.RSVP.hash({
foos: store.find('foos'),
bars: store.find('bars')
});
},
setupController: function(controller, model) {
controller.set('foos', model.foos);
controller.set('bars', model.bars);
}
});
Ember.RSVP.hash
will return a promise that waits on the promise values of all properties of the passed object, and will then fulfill with an object with the same property names and the promise fulfillment results as values.
By overriding setupController
, you can determine what properties are set on the controller and with what values.
Upvotes: 2