1

重複の可能性:
他のコントローラーからのコントローラーへのアクセス

古いルーター スタイルの Ember pre2 以前では、ルーターから他のコントローラーを取得できたので、私が というコントローラーにいた場合、次のPeopleControllerようなことができました。

App.PeopleController = Ember.Controller.extend({
     some_computed_property: (function() {
          return this.get('target.otherController.property_i_want');
     }).property('target.otherController.property_i_want')
});

またはデバッグコンソールから

> App.router.get('otherController.property_i_want')

これらの両方が機能しました。Pre4 / 新しいルーティング スタイルはこれを破るようです。新しいルーターと pre4 でこの機能を利用するにはどうすればよいですか?

4

4 に答える 4

3

同様の質問をしました。現在のバージョンで依存関係を宣言できます。

他のコントローラーからコントローラーへのアクセス

于 2013-01-22T23:15:45.237 に答える
3

私は恐ろしいハックをしました:

Em.Route.reopen({init:function(){
  window.App.currentRoute = this;
  this._super.apply(this,arguments);
}})

次のようなことができます。

App.currentRoute.controllerFor('something');
App.currentRoute.target...

EDIT:現在、emberはコントローラーの「ニーズ」の定義をサポートし、ルックアップ用のコンテナーを公開しています:

App.__container__.lookup("controller:application").get("someProperty")

App.ApplicationController = Em.Controller.extend({
    needs: ["authentication","notifications"],
    init: function(){
       this._super.apply(this,arguments)
       console.log(this.get("controllers.authentication"), this.get("controllers.notifications"))
    }
})
于 2013-01-22T23:21:25.337 に答える
2

コンソールからコントローラーにアクセスするにはdebugger;、コードで設定し、ブラウザーを更新します。これにより、ステートメントを設定した場所で実行が停止debuggerし、次を使用して、現在のスコープ内でコントローラーにアクセスできます。

this.controllerFor('abs');

これは、テンプレートのデバッグにも非常に役立ちます。挿入でき{{debugger;}}、コンソールでテンプレートの全範囲にアクセスできます。たとえば、controller自分viewが何であるかを調べてみてください。

于 2013-01-23T05:45:01.090 に答える
2

試すthis.controllerFor('other').get('property_i_want')

http://emberjs.com/guides/routing/setting-up-a-controller/の最後の部分を参照してください。

于 2013-01-22T21:22:35.263 に答える