Neil Neil - 1 month ago 40
Javascript Question

Accessing Ember-CLI Nested Controllers

This is my directory structure:

controllers/
---- restaurant/
----items.js
---- index.js
---- restaurant.js


And my router declaration:

this.route("restaurants",{
path: "/restaurants"
});

this.resource("restaurant", {
path: "/restaurants/:restaurant_id"
}, function() {
this.resource("items", {
path: "/items"
});
});


My Items controller (located in restaurants/items.js) begins with the following:

export default Ember.ObjectController.extend({
needs: ["restaurant"],
restaurant: Ember.computed.alias('controllers.restaurant.model')


and is then followed by an action to add the item under hte restaurant.

However, I keep getting hte error saying "restaurant" needs to be added to "needs":


ReferenceError: (generated items controller)#needs does not include
restaurant
. To access the restaurant controller from (generated items controller), (generated items controller) should have a
needs
property that is an array of the controllers it has access to


This is my setup (Ember-CLI 0.1.2 with Ember 1.7) - As i'm using the fireplace adapter to work with firebase, I don't think it supports upgrading Ember (from what I've tried).

DEBUG: -------------------------------
DEBUG: Ember : 1.7.0"
DEBUG: Ember Data : 1.0.0-beta.10"
DEBUG: Handlebars : 1.3.0"
DEBUG: jQuery : 1.11.2"
DEBUG: Fireplace : 0.2.9"
DEBUG: -------------------------------


I've tried the other Stackoverflow answers (e.g. How to communicate between controllers in Ember.js) but they don't seem to help.

Does anyone know what's going on here? Thanks!

Answer

As of Ember-CLI v0.2.1 + Ember v1.10.0 (could work for earlier versions; but I haven't tried), this is how you do it:

export default Ember.ObjectController.extend({
    needs: ["restaurant/items"],
    ...

To access actions, you'd do this:

actions: {
    myAction: function(arg1, arg2) {
      this.get('controllers.restaurant/item').send('someItemActionYouDefine', arg1, arg2);
    }
}
Comments