mardi 14 février 2017

willTransition is getting called twice

Goal: prevent transitioning to another route if the current route's model has been changed (i.e. user has updated some fields but not saved, i.e. isDirty === true.

Setup: I'm using pretty much this exact code from EmberJS.com's Routing guide.

export default Ember.Route.extend({
  actions: {
    willTransition(transition) {
      if (this.controller.get('userHasEnteredData') &&
          !confirm("Are you sure you want to abandon progress?")) {
        transition.abort();
      } else {
        // Bubble the `willTransition` action so that
        // parent routes can decide whether or not to abort.
        return true;
      }
    }
  }
});

In my controller, userHasEnteredData is just a computed property that watches the model's isDirty property.

Issue: When I choose to cancel from the confirm box (i.e. "Cancel transitioning so I can finish editing"), then the confirm box pops up again. Canceling again makes it go away for good, but I don't know why it's getting hit twice. If I instead say "ok" in the confirm the first time, it goes ahead and transitions, without popping up the confirm again. It's only when canceling the first time that it pops up immediately again.

I tried to replicate on ember-twiddle.com, but it worked fine there, calling willTransition only once. If it's indeed being called twice in my code, I can't figure out why, since I've checked and double-checked, and there's nothing I can see that's different that would cause the hook to be called again after transition.abort() runs.

Any clues?




Aucun commentaire:

Enregistrer un commentaire