How to save a model and ignore it's return payload


#1

I have a action that produces a lot of PUTs to my api for a single model. I debounce it to eliminate as many calls as possible. However, it’s still possible that the model will be changed by the user while a record is in flight. When the save finishes, the returned payload from the server overrides any changes the user may have done.

So I want the save to proceed, but I want to ignore the return payload. Right now, my thought is to grab the models snapshot and adapter and call updateRecorddirectly (rather than model.save(), but I’m wondering if there is a way to do this that doesn’t involve private snapshot apis.

Thanks!


#2

Thanks to @lifeart in Discourse, I have an answer. DS.Model.save() accepts an options hash. If that has has adapterOptions in it, that will be passed to the adapter. So you can do a model save call like this:

model.save({ 
  adapterOptions: { 
    ignoreResponsePayload: true 
  }
})

Then override your model’s adapter with something like this:

import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({
  updateRecord(store, type, snapshot) {
    return this._super(...arguments).then(payload => {
      if (snapshot.adapterOptions.ignoreResponsePayload) {
        return null;
      } else {
        return payload;
      }
    });
  }
});


#3

This is a great question! Thanks for recording your solution here for posterity :grinning:


#4

Another possible solution is to use ember-changeset because you apply the changeset in the save action just prior to saving, the underlying record can change as much as it likes and you have a buffer of user input in the changeset that won’t get overridden.

Disclaimer: while we use ember-changeset frequently, I haven’t personally tried it with an implementation where you save onInput. I think it should work but ymmv.