gpt4 book ai didi

ember.js - 在 Ember deleteRecord 中添加有效负载

转载 作者:行者123 更新时间:2023-12-05 00:19:29 25 4
gpt4 key购买 nike

每当他尝试删除项目时,我都要求在有效负载中包含用户的评论。到目前为止,我有这个:

let remarks = this.get('remarks');
let id = this.get('itemID');

this.store.findRecord('item', id).then(function (selectedItem) {

// TODO - DELETE doesn't accept payload in body?

selectedItem.destroyRecord({remarks:remarks}).then(function(response){

Ember.debug('delete successful:'+JSON.stringify(response));

Ember.$('#confirmDelete').modal('hide');
Ember.$('#remarks').val('');

context.set('successful', true);
context.set('message', context.get('i18n').t('success.role.delete'));

}).catch(function(error){
Ember.debug('delete failed:'+JSON.stringify(error));

Ember.$('#confirmDelete').modal('hide');
Ember.$('#remarks').val('');

context.send('showErrors', error);
});
});

它不起作用。在模型中设置备注值也是如此:
...
this.store.findRecord('item', id).then(function (selectedItem) {

selectedItem.set('remarks', remarks);

selectedItem.destroyRecord().then(function(response){
...

我试图覆盖 deleteRecord但我不知道从哪里开始或如何去做。

有人有想法吗?谢谢!

最佳答案

通过使用以下 mixin 扩展应用程序适配器,您可以轻松实现这种行为:

/* app/mixins/delete-with-playload.js */

import Ember from 'ember';

export default Ember.Mixin.create({
deleteRecord(store, type, snapshot) {
var id = snapshot.id;
var data = {};
var serializer = store.serializerFor(type.modelName);

serializer.serializeIntoHash(data, type, snapshot);

return this.ajax(this.buildURL(type.modelName, id, snapshot, 'deleteRecord'), "DELETE", {
data
});
}
});

然后只需将其添加到您的应用程序适配器

/* app/adapters/application.js */

import RestAdapter from 'ember-data/adapters/rest';
import DeleteWithPayloadMixin from '../mixins/delete-with-payload';
export default RestAdapter.extend(DeleteWithPayloadMixin);

这将产生与 PUT 的有效载荷相同的有效载荷。方法,表示以下形式的有效负载:

{
"<model-name>": {
// model's serialized attributes
}
}

现在您要做的就是在删除之前在记录上设置所需的属性,然后销毁记录。

model.setProperties({
deleteReason: 'whatever'
});
model.destroyRecord();
/*
results a DELETE request when requestBody is "{
"<model-name>": {
...
"deleteReason": "whatever"
...
}
}"
*/

关于ember.js - 在 Ember deleteRecord 中添加有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35819980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com