升級到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