问题
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