How to save an Ember Data record without sending it to the server?

So, the Ember data model has deleteRecord()one that executes destroyRecord()without sending it to the server.

How can I do save()it without sending it to the server?

The reason I need is because I use a custom service to batch write multiple records of different types (models) in a single request. I successfully send requests and the records are saved on the server.

But since the request does not go through the Ember Data pipeline, the response from the server will be discarded unless I manually process it.

Basically, I have this in the service:

// Accepts an array of records of mixed types,
// both existing and new
batchSave (records) {             

  this
    .customAjax(records)          // The records are persisted
    .then(payload => {            // Response from the backend with updated records
      store.pushPayload(payload); // Now all records have been updated with their current state

      // Now all the records are in their current state.
      // But they are still dirty!
      // How do I mark them clean and saved
    });

I saw this one , but it seems to throw away the dirty attributes, while I want the dirty attributes to become clean.

store.didSaveRecord(), .

+4
3

@Tom Netzband .

-, mixin :

// mixins/prevent-save-adapter.js
export default Ember.Mixin.create({
  preventSave: false,

  updateRecord(store, type, snapshot) {
    if (!this.get('preventSave')) 
      return this._super(store, type, snapshot);
    this.set('preventSave', false);
    return true;
  }
});

:

// mixins/prevent-save-model.js
export default Ember.Mixin.create({
  saveWithoutSave() {
    var modelName = this.constructor.modelName;
    var adapter   = this.adapterFor(modelName);

    adapter . set('preventSave', true);
    return this.save();
  }
});

:

// adapters/post.js
export default ApplicationAdapter.extend(PreventSaveAdapter);

:

// models/post.js
export default DS.Model.extend(PreventSaveModel, {
  ...
);

:

// controllers/some-controller.js
export default Ember.Controller.extend({

  actions: {
    someAction () {
      (...)
      post.saveWithoutSave();
    }
  }
});

.

+3

: , , - .

: torazaburo .


. , true updateRecord, preventRequest: true.

:

// services/prevent-request.js
export default Ember.Service.extend({
  prevent: false // default
});

// adapters/post.js
export default ApplicationAdapter.extend({
  preventSave: Ember.inject.service(),

  updateRecord (store, type, snapshot) {
    if (this.get('preventSave.prevent')) {
      this.set('preventSave.prevent', false);
      return true;
    }

    this._super(store, type, snapshot);
  }
});

// controllers/some-controller.js
export default Ember.Controller.extend({
  preventSave: Ember.inject.service(),

  actions: {
    someAction () {
      (...)
      this.get('preventSave').set('prevent', true);
      post.save();
    }
  }
});
+2

Ember Guides store.createRecord() , .

:

store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum'
});

store this.store.

, , save().

:

post.save(); // => POST to '/posts'

isDirty , , . , ( ) .

:

  • uncommitted: The vault has not yet transferred the saved record.
  • inFlight: The repository transferred the saved record, but the adapter has not yet confirmed success.
  • invalid: The record has incorrect information and cannot be sent to the adapter yet.

If you want to make the recording clean, try something like this (untested by me):

record.get('stateManager').send('becameClean'); 
0
source

Source: https://habr.com/ru/post/1616041/


All Articles