lundi 29 juin 2015

ember route event didTransition timing

I want to open a modal in a route after transitioning to it. My guess is to use the "didTransition" event. In the called method (a util) I refer to an Ember.View object.

My route actions:

actions: {
  openModal: modal.open,
  closeModal: modal.close,
  toggleModal: modal.toggle,

  didTransition: function() {
    this.send('openModal', 'choose');
  }
}

The problem is using:

didTransition: function() {
    this.send('openModal', 'choose');
}

doesn't work (because the view object is undefined, see further down in utils), but using:

didTransition: function() {
    setTimeout(function() {
        self.send('openModal', 'choose');
    }, 0);
}

does work.

Why does it not work with the standard call? I guess it's a problem with synchronicity.

The utils looks as following:

import Ember from 'ember';

export default {
  open: function(id) {
    console.log('utils open');
    var modal = Ember.View.views[id];

    // test output for debugging
    console.log(modal);

    modal.send('open');
  },
  close: function(id) {
    var modal = Ember.View.views[id];
    modal.send('close');
  },
  toggle: function(id) {
    var modal = Ember.View.views[id];
    modal.send('toggle');
  }
};

Am I missing something or is there a better method for doing this? I am using ember.js 1.12.0.




Aucun commentaire:

Enregistrer un commentaire