jeudi 24 novembre 2016

Ember 2.5 observe session property changes

I've monkey-patched my router to store the current route components in a session variable:

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]);

    }
});

I know that this is not the cleanest of solutions, but writing the session to the console proves that at least those parameters are set.

In my controller, I'd like to listen for changes in these parameters, but somehow this does not work:

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");
    })
});

i.e. "route changed" is never printed to the console.

This seems quite an easy fix, but I haven't been able to find a solution on SO.

Thanks!




Aucun commentaire:

Enregistrer un commentaire