“请确保该控制器已被实例化为容器” [英] "Please ensure this controller was instantiated with a container"

查看:103
本文介绍了“请确保该控制器已被实例化为容器”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 未捕获错误:断言失败:`&((Ember.ObjectController的子类)):ember947>指定需要,但没有容器。请确保此控制器已被实例化为容器。 

如果由于某种原因控制器没有容器,我该如何提供一个容器?上下文是下面的,但这本质上是被问到的问题。



上下文显然不是一个简单的方式为Ember.CollectionView中的各个项目提供控制器,这是 ember.js / issues / 4137 中概述的问题。 p>

似乎获取项目控制器的唯一方法是在CollectionView的内部itemViewClass声明的init方法中内联它们(由该票的发起人确认) :

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

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

//控制器收集中的单个项目

actions:{
//特定于这些项目的操作
}
}
})。create {
content:content,
});
this.set('controller',controller);
this._super();
}
})
});

所以这样做,但是如果你向这个控制器添加一个需要属性,它会给出错误关于没有容器这些项目控制器将观察外部控制器上的属性,因此我需要需要。那么我如何使用容器来实例化控制器?或者在实例化之后进行实例化处理?

解决方案

访问 App .__ container __ 通常是反对的。所有的核心对象,如视图,控制器,路由,都应该被容器实例化。在这种情况下,他们还将有一个容器属性(简单的JS属性,而不是Ember属性),您可以使用它来实例化其他对象,这些对象又可以访问容器。



所以而不是

  Ember.ObjectController.create (...)

尝试

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

如果容器未定义,则必须上链,并确保您从这个容器中出来的任何对象也从容器中出来。


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?

解决方案

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.

这篇关于“请确保该控制器已被实例化为容器”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆