Sparda
Sparda

Reputation: 610

EmberJS - Accessing multiple models in a single route

I have 2 different modules listings and contacts. Each has their own route and controller. Now there is a new requirement where partial information from contacts must be visible in the listings page. I'm having a hard time figuring out how to access the contacts model inside the listings route.

I went through the Ember docs and I came across 2 ways to access a different controller inside a route. One being the needs hash inside the controller and the other is the setupController hash inside the route. I tried both but failed. I'm most likely doing something wrong. I created a JSFiddle below. Would really appreciate if anyone can point me in the right direction.

http://jsfiddle.net/NAFq5/3/

PS: I'm not using Ember Data

Upvotes: 1

Views: 84

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

It just takes a little getting used to the router. Routes are only hit if they correspond to the current url. So you wouldn't want to depend on a controller (nor the model on that controller) if it weren't a direct ancestor in the router's tree structure.

Neither listings nor contacts should depend on each other since their urls would be /listings or /contacts and they are only siblings.

CRM.Router.map(function() {
    this.resource('listings');
    this.resource('contacts');
});

Listings could depend on contacts (contacts is an ancestor), but not the other way around (/contacts or /contacts/listings). You'll notice you can't ever get to listings without going through contacts, so contacts will always be available for listings to depend on and use.

CRM.Router.map(function() {
    this.resource('contacts', function(){
      this.resource('listings');
    });
});

If you need multiple models in a single route, you might want to check out EmberJS: How to load multiple models on the same route? (be sure to read the second answer since the first can lead to unwanted side-effects).

Example: http://jsfiddle.net/NAFq5/5/

Upvotes: 1

Related Questions