如何从一个视图的init()方法获取任何控制器实例? [英] How to get any controller instance from init() method of a view?
问题描述
var controller = App.get('router')。get('firstController');
但现在这会抛出以下错误。
未捕获TypeError:无法调用未定义的方法'get'
这可能是因为它不能获得路由器对象。现在如何获取与视图无关的控制器实例?或者如何获取路由器对象
需求功能允许控制器访问其他控制器,这允许控制器查看以访问其他控制器。 (对Ember中需求的一个很好的解释: http:// darthdeus.github.com/blog/2013/01/27/controllers-needs-explained/ )
正如在1.0.0rc中的View的init函数中禁用访问控制器,调用 init()
时,视图的控制器
属性尚未设置,因此您需要在视图的生命周期中稍后访问控制器
。这可能是例如 willInsertElement()
或 didInsertElement()
钩子。
这是一个示例,演示使用需求从一个视图访问另一个控制器:
http://jsbin.com/ixupad/186/edit
App = Ember.Application.create({});
App.ApplicationController = Ember.Controller.extend({
doSomething:function(message){
console.log(message);
}
});
App.IndexView = Ember.View.extend({
templateName:'index',
init:function(){
this._super();
//不起作用,控制器未设置为此视图,但请参阅:
// https://stackoverflow.com/questions/15272318/cannot-access-controller-in-init-function- of-view-in-1-0-0rc
//this.get('controller.controllers.application').doSomething(\"from view init);
},
willInsertElement :function(){
this.get('controller.controllers.application')。doSomething(from view willInsertElement);
},
clickMe:function(){
this.get('controller.controllers.application')。doSomething(from clickMe);
}
});
App.IndexController = Ember.Controller.extend({
需要:['application']
});
I am migrating my project from older version of EmberJS. In some places i used to get controller instance which is not related to the view, by using following in any view's init() method:
var controller = App.get('router').get('firstController');
But now this throws following error.
Uncaught TypeError: Cannot call method 'get' of undefined
This may be because it is not able to get the Router object. Now how to get controller instance which is not related to the view? or how to get the Router Object
The 'needs' feature allows a controller to access to other controllers, which allows a controller's view to access other controllers. (a good explanation of needs in Ember: http://darthdeus.github.com/blog/2013/01/27/controllers-needs-explained/)
As explained in Cannot access Controller in init function of View in 1.0.0rc, the controller
property of a view is not yet set when init()
is called, so you will need to access controller
at a later time in the view's life cycle. This could be the willInsertElement()
or didInsertElement()
hooks, for example.
Here is an example demonstrating using needs access another controller from a view:
http://jsbin.com/ixupad/186/edit
App = Ember.Application.create({});
App.ApplicationController = Ember.Controller.extend({
doSomething: function(message) {
console.log(message);
}
});
App.IndexView = Ember.View.extend({
templateName: 'index',
init: function() {
this._super();
// doesn't work, controller is not set for this view yet see:
// https://stackoverflow.com/questions/15272318/cannot-access-controller-in-init-function-of-view-in-1-0-0rc
//this.get('controller.controllers.application').doSomething("from view init");
},
willInsertElement: function() {
this.get('controller.controllers.application').doSomething("from view willInsertElement");
},
clickMe: function() {
this.get('controller.controllers.application').doSomething("from clickMe");
}
});
App.IndexController = Ember.Controller.extend({
needs: ['application']
});
这篇关于如何从一个视图的init()方法获取任何控制器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!