Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current route's model in Ember component

Tags:

ember.js

I'm writing a component in which I need to access the current route's model; in the component's template I would like to do something like:

{{#each color in model}}
    ...
{{/each}}

I will use this component in different routes, with different models

How can I access the current route's model inside the component?

Ember.Component.extend({
    didInsertElement: function() {
        console.log(this.get('controller')    //this is not the route's controller
        console.log(this.get('controller').get('model'));   //undefined of course
    }
});
like image 630
Cereal Killer Avatar asked Oct 04 '14 20:10

Cereal Killer


People also ask

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.

What is component in Ember JS?

Ember components are used to turn markup text and styles into reusable content. Components consist of two parts: a JavaScript component file that defines behavior, and its accompanying Handlebars template that defines the markup for the component's UI. Components must have at least one dash in their name.

What is service in Ember?

A Service is an Ember object that lives for the duration of the application, and can be made available in different parts of your application. Services are useful for features that require shared state or persistent connections. Example uses of services might include: User/session authentication. Geolocation.


1 Answers

Pass it into the component.

{{my-comp model=model foo=model bar=model}}

In the example above, within the scope of your component model, foo, and bar would be the model.

like image 192
Kingpin2k Avatar answered Sep 21 '22 20:09

Kingpin2k