如果路由或模型发生变化,则稍后取消ember .run. js

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

本文关键字:取消 ember js run 路由 模型 变化 如果      更新时间:2023-09-26

给定以下代码:

如果用户操作中的特定操作发生,则调用该函数。

现在我想"停止"这个和类似的功能"如果"用户离开路由(url)。

我在API文档中找不到任何方法来调用"在"路由更改之前的方法。

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

你可以在你的路由动作中定义willTransition钩子。具体的案例记录在烬指南

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