15

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 コントローラーにアクセスする適切な方法は何ですか。

4

1 に答える 1

30

最新バージョンの 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 と users が必要なため、これらのコントローラーはその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://github.com/emberjs/ember.js/blob/master/packages/ember-application/tests/system/controller_test.jsを参照してください

于 2013-01-17T21:52:11.083 に答える