2016-02-19 19 views
4

我正在使用Ember數據和現在默認的json-api適配器的Ember應用程序。如何從json-api服務器的200響應中調用ember數據的destroyRecord()

按照JSON-API SPEC(http://jsonapi.org/format/#crud-deleting)刪除記錄時,您的服務器應該返回200響應,如果刪除成功,服務器只是一個頂級meta鍵響應。

我當前的服務器做到這一點,我試圖找出如何使用Ember Data的model.destroyRecord()方法訪問頂級元對象中的數據。

myModel.destroyRecord().then(function(model){ 
    // the returned value is the model. How can I get the actual metadata 
    // returned by the server? 
}); 

服務器響應包含以下信息:究竟是什麼被刪除,看起來像這樣:

{ 
    "meta": { 
     num-deleted-a: 10, 
     num-deleted-b: 100, 
     num-deleted-c: 200 
    } 
} 

我想獲得這些信息,以便我可以將它顯示給用戶。

謝謝!

我使用以下版本:

Ember    : 2.2.0 
Ember Data  : 2.3.3 
jQuery   : 1.11.3 

回答

1

灰燼不支持meta對於目前單一的模式請求(findsavedestroyRecord)!

如果你想要這個,你必須鉤入燼內部。

以下代碼使用來自於ember 2.3的ember內部函數,並可能在未來版本中崩潰!

store上有未公開的_metadataFor函數,它爲您提供給定類型的最後一個元數據。我使用自定義的初始化總是將它保存到Model

import Ember from 'ember'; 
import DS from 'ember-data'; 
const {set} = Ember; 
export function initialize(application) { 
    DS.Model.reopen({ 
    meta: null, 
    didCommit() { 
     this._super(...arguments); 
     set(this, 'meta', this.store._metadataFor(this.constructor.modelName)); 
    } 
    }); 
}; 

export default { 
    name: 'meta', 
    initialize: initialize 
}; 

在這之後,你可以做model.save().then(() => console.log(model.get('meta')))model.destroyRecord.then(() => console.log(model.get('meta')))

Maybe checkout this ember-twiddle.

+0

感謝和抱歉的回覆遲!在跟蹤代碼時我偶然發現了'_metadataFor'屬性,並能夠使用它。我喜歡你將它直接添加到模型中。 – Sarus

+0

只是FYI的人來到這個使用Ember 2.6。這不再顯示工作。 – Sarus

4

升級到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')); 
}); 
+0

normalizeResponse(store,primaryModelClass,payload,id,requestType)payload.data.attributes.meta = payload.meta || {}; 返回this._super.apply(this,arguments); } //請參閱:https://guides.emberjs.com/v2.1.0/models/customizing-serializers/#toc_customizing-srializers – GK100

相關問題