Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember - How to get route model inside route action

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)?

like image 549
Manu Benjamin Avatar asked Feb 25 '16 14:02

Manu Benjamin


People also ask

How to generate route in ember?

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.

What is route action in Ember?

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.

What is a controller in Ember?

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.

What is a model in Ember?

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.


2 Answers

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:

  1. That action is sent from the controller/template. Can't you pass the model as a parameter? That way you could access the model through the function arguments.
  2. If you absolutely need to, this.modelFor(this.routeName); returns the model for the current route.
  3. You can access the model in route through the controller like this.controller.get('model').
  4. You could also implement the setupController hook and access there the model. You can then do things like this.set('employeeModel', model); for later access.
like image 137
miguelcobain Avatar answered Oct 15 '22 11:10

miguelcobain


this.get('context')

gives you access to the model in the route action.

like image 28
steve_gallagher Avatar answered Oct 15 '22 13:10

steve_gallagher