Search code examples
ember-data

Passing parameters to save()


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})

Solution

  • 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.