Passing parameters to save()

南笙酒味 提交于 2020-02-27 23:27:39

问题


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

回答1:


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.




回答2:


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`.
    // ...
  }
});


来源:https://stackoverflow.com/questions/25540769/passing-parameters-to-save

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!