Purrell
Purrell

Reputation: 12881

"Please ensure this controller was instantiated with a container"

Uncaught Error: Assertion Failed: `<(subclass of Ember.ObjectController):ember947> specifies `needs`, but does not have a container. Please ensure this controller was instantiated with a container.

If for some reason a controller doesn't have a container, how can I provide it with one? Context is below, but that is essentially the question being asked.

The context is that there apparently is not a straightforward way of providing controllers for the individual items in Ember.CollectionView, a problem which is outlined at ember.js/issues/4137.

It seems the only way to get item controllers is to declare the them inline in the init method for an inline itemViewClass declaration of the CollectionView (as confirmed by the originator of that ticket):

var someCollectionView = Ember.CollectionView.extend({
  itemViewClass: Ember.ListItemView.extend({
                     templateName: "foo-item",

                     init: function(){
                       var content = this.get('content');
                       var controller = Ember.ObjectController.extend({

                                            // controller for individual items in the collection

                                            actions: {
                                                // actions specific to those items
                                                }
                                            }
                        }).create({
                            content: content,
                        });
                        this.set('controller', controller);
                        this._super();
                     }
                  })
});

So this works, however if you add a "needs" property to this controller, it gives the error about no container. These item controllers will be observing a property on an external controller, so I need the "needs". So how do I instantiate the controller with the container... or hack it in after instantiation?

Upvotes: 1

Views: 1151

Answers (2)

Arne Brasseur
Arne Brasseur

Reputation: 1518

Accessing App.__container__ is generally adviced against. All core objects like views, controllers, routes, should have been instantiated by the container. In that case they will also have a container property (plain JS property, not an Ember property), that you can use to instantiate other objects, which in turn will have access to the container.

So instead of

Ember.ObjectController.create(...)

try

this.container.lookupFactory('controller:object').create(...)

If container is undefined you'll have to go up the chain, and make sure whatever object you're calling this from is also coming out of the container.

Upvotes: 2

Purrell
Purrell

Reputation: 12881

It looks like you can do

           ...
                        }).create({
                            content: content,
                            container: App.__container__
                        });
                        this.set('controller', controller);
                        this._super();
                     }
                  })
});

Upvotes: 0

Related Questions