如果路线或模型发生变化,Ember.js将取消Ember.run.later

时间:2014-05-06 07:41:29

标签: javascript ember.js

给出以下代码:

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

现在我想“停止”这个和类似的功能“如果”用户离开路线(网址)。

我无法在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

1 个答案:

答案 0 :(得分:1)

您可以在路线操作中定义 willTransition 挂钩。具体案例记录在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;
      }
    }
  }
});