Using "needs" in component

I’m trying to use “needs” in an ember component but getting the error "Property set failed: object in path "controllers.categoryManager" could not be found or was destroyed." A sample of problem can be found on JSBIN, Ember Starter Kit, Is it that we cannot use “needs” in components ?

Part of code:

App.CategoryManagerController = Ember.Controller.extend({
  selectedCategory: null,
});

App.BlogPostComponent = Ember.Component.extend({
    needs: ['categoryManager'],
    selectedCategory: Ember.computed.alias('controllers.categoryManager.selectedCategory'),
    actions:{
        selectedCategory: function (){
            this.set('selectedCategory',1);
        }
    }
});

Yes, you can’t use needs in a component as they are meant to be completely isolated.

You’ll need to pass in your collaborators in the template when you setup the component:

{{blog-post selectedCategory=category}}

Then in the controller for that template you could use needs to get hold of the selected category.

1 Like

it works, cool, thanks :slight_smile:

You can also check the new injector API, which serves a similar purpose as needs, but not limited to controllers.