Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to save()

Tags:

ember-data

Is it possible to pass parameters like this? I need to pass some information that is not part of the model itself.

myModel.save({site : 23})
like image 360
jax Avatar asked Aug 28 '14 04:08

jax


2 Answers

You can pass options as of Ember Data 2.2. However, you have to remember to pass your options under the adapterOptions property. For example,

myModel.save({
  adapterOptions: {
    site: 23
  }
});

Inside either of DS.Store#findAll, DS.Store#findRecord, DS.Store#query, DS.Model#save and DS.Model#destroyRecord, one of the parameters should now have adapterOptions. In the case of DS.Model#save, you can override updateRecord in your adapter:

export default DS.Adapter.extend({
  updateRecord(store, type, snapshot) {
    // will now have `snapshot.adapterOptions`.
    // ...
  }
});
like image 77
nucleartide Avatar answered Oct 21 '22 23:10

nucleartide


It is possible if you:

  • add a 'volatile' attribute to your model,
  • define a custom model serializer, and override its serializeIntoHash method.

For instance:

App.Model = DS.Model.extend({
  //...
  site: DS.attr('number', { serialize: false })
});
App.ModelSerializer = DS.RESTSerializer.extend({

  serializeIntoHash: function(hash, type, record, options) {
    this._super(hash, type, record, options);

    Ember.merge(hash, {
      'site': record.get('site')
    });
  }
});

See this comment, this is the correct way to achieve your goal.

like image 44
Mike Aski Avatar answered Oct 22 '22 00:10

Mike Aski