mercredi 25 mai 2016

Ember - Object property observers

It seem like I can't use arrow functions as observers, anonymous methods works fine. Am I missing something crucial, why does the context differ?

ember.debug.js:32096 TypeError: _this3.get is not a function
    at .sessionChanged (session.js:77)
    at Object.applyStr (ember.debug.js:23331)
    at Object.sendEvent (ember.debug.js:16842)
    at ObserverSet.flush (ember.debug.js:20171)
    at endPropertyChanges (ember.debug.js:20682)
    at Object.changeProperties (ember.debug.js:20707)
    at Object.setProperties [as default] (ember.debug.js:21786)
    at exports.default._emberMetalMixin.Mixin.create.setProperties (ember.debug.js:35465)
    at invalidate (session.js:98)
    at session.js:42

Anonymous function - Works.

  /**
   * Session event observer.
   */
  sessionChanged: Ember.observer('user', function() {
    // Get the user value
    const user = this.get('user');

    // Get the session token
    const token = this.get('token');

    console.log(user, token);
  }),

Observer method using arrow function - Throws exception.

  /**
   * Session event observer.
   */
  sessionChanged: Ember.observer('user', () => {
    // Get the user value
    const user = this.get('user');

    // Get the session token
    const token = this.get('token');

    console.log(user, token);
  }),




Aucun commentaire:

Enregistrer un commentaire