5

残り火リストと編集フォームがあります。リストの選択された項目が変更されるたびに、編集フォームは変更を破棄し、新しいモデルをロードします。

私の問題は、deactivateイベントが発生しないため、変更を破棄する方法がないことです。

たとえば、url/favs/123/editからurl/favs/456/edit非アクティブ化(および終了)イベントに移行しても発生しません。したがって、変更を適切に破棄する方法はありません。

これが私が参照している私のコードの一部です:

App.Router.map(function() {
    this.resource('favs', { path: '/favs' }, function() {
        this.route('new');
        this.route('edit', { path: ':fav_id/edit' })
    });
});

[...]

App.FavsEditRoute = Ember.Route.extend({
    deactivate: function() {
        var model = this.get('currentModel');
        if(model && model.get('isDirty')) {
            model.get('transaction').rollback();
        }
    },

    model: function(params) {
        return App.Fav.find(params.fav_id);
    },
});
4

3 に答える 3

4

I'd recommend using the willTransition route action. It currently appears to be advertised as the solution in the Ember Guide:

https://guides.emberjs.com/release/routing/preventing-and-retrying-transitions/

Aside from being in the public API, this approach has the advantage that you can prompt the user to confirm if they actually want to abandon the change, nullifying the transition if they say no.

For example:

App.FavsEditRoute = Ember.Route.extend({
  ...
  actions: {
    willTransition: function(transition) {
      controller = this.controllerFor('fav');
      if (controller.get('isDirty') &&
          !confirm("Are you sure you want to abandon progress?")) {
        transition.abort();
        return false;
      } else {
        controller.get("content").rollback();
        return true;
      }
    }
  }
});
于 2013-10-04T18:31:58.000 に答える
2

ルートが完全に離れると、非アクティブ化フックが実行されます。したがって、ルートのcontextDidChange関数を上書きすることをお勧めします。これがEmberSourceからの抜粋です。

Ember.Route = Ember.Object.extend({
    /**
    @private

    Called when the context is changed by router.js.
  */
  contextDidChange: function() {
    this.currentModel = this.context;
  }
});

私はこれを行うことをお勧めします:

App.FavsEditRoute = Ember.Route.extend({
    deactivate: function() {
        this.doRollback();
    },
    contextDidChange: function() {
        this.doRollback();
        this._super();
    },
    doRollback: function(){
        var model = this.get('currentModel');
        if(model && model.get('isDirty')) {
            model.get('transaction').rollback();
        }
    }
});
于 2013-03-24T11:39:39.163 に答える
1

In Ember 2.2 the correct code (in route) is:

actions: {
  saveScene: function (scene) {
    var _that = this;

    scene.save().then(function (response) {
      // on saveScene action, go to list route
      _that.transitionTo('scenes');
    });
  },

  willTransition: function (transition) {
    // on transition, if model has unsaved changes, revert them
    var model = this.controller.get('model');
    if (model && model.get('hasDirtyAttributes')) {
      model.rollbackAttributes();
    }
  }
}
于 2015-12-18T08:43:55.737 に答える