簡體   English   中英

灰燼數據。 如何發送到模型中未定義的服務器屬性?

[英]Ember Data. How to sent to the server attributes which are not defined in model?

如果我從服務器收到額外的屬性(未在模型中定義),我不會將它們存儲在 ember 模型中。 因此,在PUTPOST我不會將它們發送到服務器。

有沒有辦法將這些額外的屬性保留為“兌現數據”並將它們發送回服務器?

您可以通過使用序列化程序來發送和接收數據來實現這一點,例如,如果您使用的是 JSONAPIAdapter,那么您可以使用如下序列化程序:

export default class YouRouteSerializer extends JSONAPISerializer {

  //for sending data
  serialize(snapshot, options) {
    let json = super.serialize(...arguments);

    //add a vlue that doesnt exist in the model
    json.data.attributes.theNewAttributeName =  'example';

    // delete a value 
    delete json.data.attributes.theAttributeNameToBeDeleted;

    return json;
  }


 //and for receiving (this is what you are looking for)

normalizeResponse(store, primaryModelClass, payload, id, requestType) {

    let extratData = payload.data.attributes.yourExtratData;
    // here store you extrat data somewhere else , localstorage for example
    // thenn
    // delete your extra data
    delete payload.data.attributes.yourExtratData;

    return super.normalizeResponse(...arguments);
  }
}

這是自定義序列化程序的參考Customizing Serializers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM