我正在使用ember.js-pre3 ember data版本11构建一个项目管理应用程序。
如何初始化几个控制器并使它们全局可用。例如,我有一个在每个状态下都需要访问的currentUser控制器和usersController。我曾经在Ember.ready函数中包含以下代码,但它不再工作。我猜我这样做的目的是为了调试。https://github.com/emberjs/ember.js/issues/1646
老方法:
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()在所有应用程序状态下访问currentUser控制器的正确方式是什么?
发布于 2013-01-18 05:52:11
在最新版本的ember (ember-1.0.0-pre.3.js)中,您可以通过声明控制器依赖项来做到这一点。一旦声明了依赖项,就可以通过controllers属性访问它。例如:
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']
});因为ApplicationController需要currentUser和用户,所以这些控制器可以通过它的controllers属性访问,并且可以从应用程序模板中使用:
<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>下面是一个有效的示例:http://jsfiddle.net/mgrassotti/mPYEX/
https://stackoverflow.com/questions/14388249
复制相似问题