Is it possible to access route model inside route action?
I am passing multiple objects inside a route model to template,
model: function() {
return {
employeeList : this.store.findAll("employee"),
employee : Ember.Object.create()
}
}
From the route action I am want to modify the route model.employee. I tried the following, but I am not getting the object.
actions:{
editAction : function(id) {
var emp = this.get("model");
console.log(emp.employee);
}
}
Can anyone give a solution to get and modify model object(employee)?
First problem is that you should return a promise from the model hook. That way, the transition will wait for the promise to Resolve. return { /*...*/};
returns an object and not a promise, even if the object itself contains promises.
The solution is to use Ember.RSVP.hash
like:
model() {
return Ember.RSVP.hash({
employeeList: this.store.findAll('employee'),
employee: Ember.Object.create()
});
}
This will return a promise that resolves when all inner promises resolve.
The second problem is that you can't use this.get('model')
in a route. If you think about it the model
property is the hook itself and not the resolved model. Solutions:
this.modelFor(this.routeName);
returns the model for the current route.this.controller.get('model')
.this.set('employeeModel', model);
for later access.