从其他控制器访问控制器 [英] Accessing controllers from other controllers
问题描述
如何初始化几个控制器并使其可用于全局。例如,我有一个currentUser控制器和usersController,我需要在每个状态下访问。我以前在Ember.ready函数中有以下代码,但它不再有效。
我想我的做法是为了调试。 https://github.com/emberjs/ember.js/issues/1646
旧方式:
window.Fp = Ember.Application.create
准备:() - >
#初始化全局集合
appController = @get'router.applicationController'
store = @get'router.store'
#用户控制器集userController在applicationController上绑定
#从服务器获取所有团队用户
#json从服务器返回包含标志isCurrent
usersController = @get'router.usersController'
usersController.set'内容',store.findAll(Fp.User)
appController.set'usersController',usersController
#CurrentUserController
#设置applicationController上的currentUserController绑定
#查找currentUser来自usersController
currentUserController = @get'router.currentUserController'
currentUserController.set'content',usersController.get('findCurrentUser')
appController.set'currentUserController',currentUserController
@_super()
什么是正确的方法o可以在所有应用程序状态下访问currentUser控制器。
在最新版本的ember(ember-1.0.0-pre.3.js)中,您可以通过声明控制器依赖。一旦依赖关系被声明,它将可以通过控制器
属性访问。例如:
window.App = Ember.Application.create();
App.ApplicationController = Ember.Controller.extend({
需要:['currentUser','users']
});
App.CurrentUserController = Ember.ObjectController.extend({
content:'mike'
});
App.UsersController = Ember.ArrayController.extend({
content:['mike','jen','sophia']
});
由于ApplicationController需要currentUser和用户,所以这些控制器可通过 / code>属性,可以在应用程序模板中使用:
< script type =text / x -handlebars>
< p>以{{controllers.currentUser.content}}}身份登录< / p>
< h2>所有用户:< / h2>
< ul>
{{#each user in controllers.users}}
< li> {{user}}< / li>
{{/ each}}
< / ul>
< / script>
这是一个工作示例: http://jsfiddle.net/mgrassotti/mPYEX/
I am building a project management app using ember.js-pre3 ember-data revision 11.
How do I initialize a couple of controllers and make them available globally. For example I have a currentUser controller and usersController that I need access to in every state. I used to have the following code in the Ember.ready function but It no longer works. I guess the way I was doing it was intended for debugging. https://github.com/emberjs/ember.js/issues/1646
Old Way:
window.Fp = Ember.Application.create
ready: () ->
# Initialize Global collections
appController = @get 'router.applicationController'
store = @get 'router.store'
# User controller sets usersController binding on applicationController
# fetches all team users from server
# json returned from server includes flag "isCurrent"
usersController = @get 'router.usersController'
usersController.set 'content', store.findAll(Fp.User)
appController.set 'usersController', usersController
# CurrentUserController
# sets currentUserController binding on applicationController
# finds currentUser from usersController
currentUserController = @get 'router.currentUserController'
currentUserController.set 'content', usersController.get('findCurrentUser')
appController.set 'currentUserController', currentUserController
@_super()
What is the proper way to have access to the currentUser controller in all application states.
In the latest version of ember (ember-1.0.0-pre.3.js) you can do this by declaring controller dependencies. Once a dependency has been declared, it will be accessible via the controllers
property. For example:
window.App = Ember.Application.create();
App.ApplicationController = Ember.Controller.extend({
needs: ['currentUser', 'users']
});
App.CurrentUserController = Ember.ObjectController.extend({
content: 'mike'
});
App.UsersController = Ember.ArrayController.extend({
content: ['mike', 'jen', 'sophia']
});
Since ApplicationController needs currentUser and users, those controllers are accessible via it's controllers
property and can be used from within the application template:
<script type="text/x-handlebars">
<p>Signed in as {{controllers.currentUser.content}}</p>
<h2>All Users:</h2>
<ul>
{{#each user in controllers.users}}
<li> {{user}} </li>
{{/each}}
</ul>
</script>
Here's a working example: http://jsfiddle.net/mgrassotti/mPYEX/
See https://github.com/emberjs/ember.js/blob/master/packages/ember-application/tests/system/controller_test.js for some examples
这篇关于从其他控制器访问控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!