ember-data和json-patch请求

jfewjypa  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(125)

ember-data是否可以在www.example.com()调用上发送json-patch PATCHmodel.save?(使用媒体类型应用程序/json-patch+json RFC 6902)
文档说是,但没有详细信息:
https://guides.emberjs.com/release/models/creating-updating-and-deleting-records/#toc_persisting-records
测试它会显示PUT请求,请求中包含整个模型。

ktca8awb

ktca8awb1#

我怀疑您的应用程序使用的是RESTAdapter,而不是JSONAPIAdapter
RESTAdapter是Ember Data 2.0之前的默认适配器,如here所述
您可以查看两个适配器updateRecord的方法:
静止适配器

/**
    Called by the store when an existing record is saved
    via the `save` method on a model record instance.
    The `updateRecord` method serializes the record and makes an Ajax (HTTP PUT) request
    to a URL computed by `buildURL`.
    See `serialize` for information on how to customize the serialized form
    of a record.
    @method updateRecord
    @param {Store} store
    @param {Model} type
    @param {Snapshot} snapshot
    @return {Promise} promise
  */
  updateRecord(store, type, snapshot) {
    const data = serializeIntoHash(store, type, snapshot, {});

    let id = snapshot.id;
    let url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');

    return this.ajax(url, 'PUT', { data });
  }

JSONAP适配器

updateRecord(store, type, snapshot) {
    const data = serializeIntoHash(store, type, snapshot);

    let url = this.buildURL(type.modelName, snapshot.id, snapshot, 'updateRecord');

    return this.ajax(url, 'PATCH', { data: data });
  }

正如您所看到的,一个使用PUT,另一个使用PATCH。JSONAPIAdapter现在是默认的,这就是文档处理PATCH请求的原因。
如果要使用PATCH而不是PUT并保留RestAdapter,则应从RestAdapter扩展并修改updateRecord方法:D
我希望它会发现你好;)

相关问题