2016-11-24 4 views
1

Iましサルパッチを当てたセッション変数に現在のルートコンポーネントを格納するために、私のルータ:エンバー2.5観察するセッションプロパティが変更さ

var Router = Ember.Router.extend({ 
    customSession: Ember.inject.service('session-custom'), 
    location: config.locationType, 

    didTransition: function() { 
     this._super(...arguments); 

     this.get('customSession').set('currentEntity', this.get('currentRouteName').split('.')[0]); 
     this.get('customSession').set('currentDetailView', this.get('currentRouteName').split('.')[1]); 

    } 
}); 

が、私はこれがソリューションのクリーンではないことを知っているが、セッションを書きます少なくともそれらのパラメータが設定されていることがコンソールに示されます。私のコントローラで

、私はこれらのパラメータの変化をリッスンしたいのですが、どういうわけか、これは動作しません。

import Ember from 'ember'; 
import ApplicationController from './application'; 

export default ApplicationController.extend({ 

    customSession: Ember.inject.service('session-custom'), 

    currentRouteNameChanged: Ember.observer('customSession.currentEntity', function() { 
     console.log("route changed"); 
    }) 
}); 

すなわち「ルートが変更され、」コンソールに出力されることはありません。

これはかなり簡単な修正と思われますが、私はSOの解決策を見つけることができませんでした。

ありがとうございます!

答えて

0

おそらくinitializerを使用して、アプリケーションのコントローラにsession-customサービスを注入することを検討してください。

sessionCustom: Ember.inject.service(), 

...との点を確認してください。そこには、いくつかの提案は...

他の場所でのルータで最初に、そして、このように、あなたのサービスを注入する従来の、ラクダの短手を使いますコードsessionCustomcustomSessionの代わりに参照してください。

次に、session-custom初期化子を作成し、アプリケーションのコントローラにサービスを注入:もちろん、

export default Ember.Controller.extend({ 
    currentRouteNameChanged: Ember.observer(
    'sessionCustom.currentEntity', function() { 
     console.log("CONTROLLER: ", this.get('sessionCustom.currentEntity')); 
    } 
), 
}); 

これらの変更を:コントローラから

export function initialize(application) { 
    application.inject('controller', 'sessionCustom', 'service:session-custom'); 
} 

export default { 
    name: 'session-custom', 
    initialize, 
}; 

観察ルート変更は、現在成功する必要がありますサービス自体からも見ることができます:

export default Ember.Service.extend({ 
    currentEntity: '', // <-- it's helpful to explicitly declare 

    currentRouteNameChanged: Ember.observer(
    'currentEntity', function() { 
     console.log("SERVICE: ", this.get('currentEntity')); 
    } 
), 
}); 

私はこの解決方法を示すためにEmber Twiddleを作成しました。

関連する問題