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)?
The map() method of your Ember application's router can be invoked to define URL mappings. When calling map() , you should pass a function that will be invoked with the value this set to an object which you can use to create routes.
ember install ember-route-action-helper. The route-action helper allows you to bubble closure actions, which will delegate it to the currently active route hierarchy per the bubbling rules explained under actions . Like closure actions, route-action will also have a return value.
In Ember. js, controllers allow you to decorate your models with display logic. In general, your models will have properties that are saved to the server, while controllers will have properties that your app does not need to save to the server.
In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.
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.this.get('context')
gives you access to the model in the route action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With