1

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

Sparda
  • 608
  • 2
  • 12
  • 25
  • I might be very wrong, but I believe `needs` just gives you dependencies between controllers, not the controller's routes (hence, not their models). All you're doing here is getting an instance of the other controller. This kind of scenario is probably better served using data modeling with `DS.hasMany('someModel')` relationships. See [here](http://stackoverflow.com/questions/18705686/emberjs-record-with-hasmany-relation-fails-to-load). – Duncan Walker Jul 14 '14 at 04:31
  • @DuncanWalker That is true but I thought we can access the model of that other route using the `content` attribute of the referenced controller. I tried accessing the content but it came up empty. – Sparda Jul 14 '14 at 04:50
  • The router sets the `content` property of the controller as the route's model. My guess is if you're not hitting the route, `content` isn't being set and, hence, `content` is empty. – Duncan Walker Jul 14 '14 at 04:51
  • @DuncanWalker So what do you think is the right approach to display multiple modules in a single route? I can try setting the `content` property manually for the `contacts` controller inside the `listings` route but not sure if it is the right way to go. And it doesn't make sense to duplicate the code that retrieves the `contacts' model either. – Sparda Jul 14 '14 at 04:57
  • If you're using hasMany relationships in your models with `async: true` on the property, you can just call `this.get('listings')` in the contacts route, and vice versa. I usually do this in the route's `setupController()` method. – Duncan Walker Jul 14 '14 at 05:03

1 Answers1

1

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/

Community
  • 1
  • 1
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • Thanks for your time. `Contacts` and `Listings` are indeed independent. I have 2 questions. I went through the answer you provided in that thread and it was very helpful but you pointed out that using the model hook will cause issues when dealing with dynamic route segments. So what is the best way to handle multiple models that are asynchronous and might involve dynamic segments? – Sparda Jul 14 '14 at 08:10
  • Also, for some reason the `RSVP.hash` is throwing an error when used as per your example. I changed it to reflect the Ember docs. In the updated fiddle above, I used the model hook. However, once the hash is returned, I have no idea how to reference it in the template. In the dev console for the fiddle, you can see the hash with correct data but the `controller` `content` property is still `undefined`. – Sparda Jul 14 '14 at 08:10
  • I updated your code with a few changes. example included above. Within the template you don't need to specify `controller.` since controller is the scope. If you pass a promise into the hash you don't need to call resolve/cast on it. Ask if you have any questions. – Kingpin2k Jul 14 '14 at 14:29