升级到2.6.1灰烬灰烬和2.6.1后,我不再能够访问store._metadataFor
属性。
要访问特定调用中的元数据,我现在覆盖模型的序列化程序,并将meta
属性添加到模型本身,该模型本身只是通过元数据。
作为一个例子,我有一个名为vote
的记录类型,保存时会返回一些元数据。
因为我做了以下的模式:
// Vote Model (/app/models/vote)
export default DS.Model.extend({
vote: DS.attr('number'),
// Since i don't provide a transform the values for meta are passed through in
// raw form
meta: DS.attr()
});
然后在串行的投票模式,我做了以下内容:
// Vote serializer (/app/serializers/vote)
import DS from "ember-data";
export default DS.JSONAPISerializer.extend({
normalizeSaveResponse(store, primaryModelClass, payload, id, requestType) {
// The metadata in the payload does get processed by default and will be
// placed into a top level `meta` key on the returned documentHash
let documentHash = this._super(store, primaryModelClass, payload, id, requestType);
// Make sure we always have an empty object assigned to the meta attribute
if(typeof(payload.meta) !== 'object'){
payload.meta = {};
}
// Move the metadata into the attributes hash for the model
documentHash.data.attributes.meta = payload.meta;
return documentHash;
}
});
注意,在上面的例子中,我只增加在对商店进行保存调用时在投票模式的元数据中。如果您想始终添加元数据,那么您将覆盖normalize
方法而不是normalizeSaveResponse
方法。
然后,您可以在保存呼叫的结果中访问meta
字段。
let vote = self.store.createRecord('vote', {
vote: voteValue
});
vote.save().then(function(result){
// this will now contain your metadata
console.info(result.get('meta'));
});
感谢和抱歉的回复迟!在跟踪代码时我偶然发现了'_metadataFor'属性,并能够使用它。我喜欢你将它直接添加到模型中。 – Sarus
只是FYI的人来到这个使用Ember 2.6。这不再显示工作。 – Sarus