user2521436
user2521436

Reputation: 299

Ember.js cancel Ember.run.later if the route or model changes

Given the following code:

This function is invoked, if a specific action within an user action occures.

Now i want to "stop" this and similar function "if" the user is leaving the route (url).

I can not find any method within the API doc to invoke a method "before" the route changes.

cronjob: function(param) {
        //Nur wenn übergebene ID gleich dem aktuellen Model ist.
        if (Ember.isEqual(param.id, this.get('id'))) {
            this.set('activeCronjob', true);
            Ember.run.later(this, function() {
                var currentPath = window.location.pathname + window.location.hash;
                if (Ember.isEqual(param.path, currentPath)) {
                    if (this.get('inBearbeitung')) {
                        console.log('Dokument mit ID' + this.get('id'));
                        console.log('Protokoll automatisch gespeichert um ' + moment().lang('de').format('hh:mm') + " Uhr");
                        this.set('savedBefore', 'Protokoll automatisch gespeichert um ' + moment().lang('de').format('hh:mm') + ' Uhr');
                        this.cronjob(param);
                    } else {
                        this.set('activeCronjob', false);
                        console.log("Model nicht oder nicht mehr im Bearbeitungsstatus mit ID: " + param.id);
                    }
                } else {
                    this.set('activeCronjob', false);
                    console.log("Path ist nicht mehr gleich, Speicherung für Eintrag mit ID: " + param.id + " beendet!");
                }
            }, 300000); //Milisekunden entspricht 5Minuten300000

Upvotes: 0

Views: 886

Answers (1)

ppcano
ppcano

Reputation: 2861

You could define willTransition hook in your route actions. The specific case is documented at Ember Guide

App.FormRoute = Ember.Route.extend({
  actions: {
    willTransition: function(transition) {
      if (this.controllerFor('form').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;
      }
    }
  }
});

Upvotes: 1

Related Questions