Search code examples
javascriptember.jscontrollersember-controllers

Ember Needs API; ReferenceError: controllers is not defined


I'm working on an Ember project. I display a list of books and descriptions passed down from a server. Then the user can click on a particular book, which links to a comment page. But, I want to display the book description on the comments page. So I learned you could use the Needs API to grab this information from the book controller.

However, I tried displaying {{controllers.book.description}} amidst the HTML and it wouldn't display anything. So I added the below action to print it out to the console and I received an error: Uncaught ReferenceError: controllers is not defined. Am I using needs wrong? Thanks for the help.

App.BookCommentController = Ember.ObjectController.extend({
    needs: ["book"],
    actions: {
        getDescription: function() {
            console.log(controllers.book.description);
        }
    }
});

Solution

  • Got it! Turns out I was using it wrong, which was difficult to diagnose as there's basically no documentation for this anywhere. In order to print it out to the console, I ended up using:

    this.get('controllers') //i.e. console.log(this.get('controllers'))
    

    in order to access the controllers variable.