gpt4 book ai didi

ember.js - `push` 更新模型返回存储的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-02 06:06:49 25 4
gpt4 key购买 nike

我正在尝试 push更新的model返回 store .我尝试了几种方法仍然失败。

请帮我理解推送model返回商店而不更新 backend api。

这是我的尝试:

    import Ember from 'ember';

export default Ember.Route.extend({
model: function(params) {

if(this.store.hasRecordForId('card-list', params.id)){
return this.store.peekRecord('card-list', params.id );
}
},
actions:{
formValidateBeforeNext:function(){

var model = this.controllerFor(this.routeName).get('model');

var modelId = this.controllerFor(this.routeName).get('model').get("id");
var oneTimeFee = this.controllerFor(this.routeName).get('model').get("oneTimeFee");
var monthlyInstalmentAmount = this.controllerFor(this.routeName).get('model').get("monthlyInstalmentAmount");


var updatedModel = JSON.parse(JSON.stringify(model));

updatedModel.type="card-list";
updatedModel.id="13";

console.log( "model would be:-" , updatedModel );
//sending just a updated model fails
let itemModel = this.store.push({'card-list': model });
//after stringfy trying to update still fails
let itemModel = this.store.push({'data': updatedModel });



// this.store.pushObject(JSON.parse(JSON.stringify(model)));

console.log( "store data", this.store.peekRecord('card-list', modelId ) )

this.transitionTo('cs2i.balance.balanceReview', {id:modelId});

}
}
});

这里有什么问题?用更新恢复模式的正确方法是什么?

更新:添加错误
Expected an object in the 'data' property in a call to 'push' for undefined, but was instance
Error

最佳答案

push方法将期望数据采用预期的格式。例如,如果您使用的是 JSONAPI。以下是预期的。

store.push({
data: {
// primary data for single record of type `Person`
id: '1',
type: 'person',
attributes: {
firstName: 'Daniel',
lastName: 'Kmak'
}
}
});

您可以通过这样做将 json 有效负载转换为预期的形式,
store.push(store.normalize('person', data));

如果您有原始 JSON 数据,那么您可以尝试 pushPayload .
this.get('store').pushPayload('card-list',data);

引用 EmberData Model Maker了解预期的结果格式。

阅读 ember 指南 models/pushing-records-into-the-store
阅读 push API doc
阅读 pushPayload doc -

关于ember.js - `push` 更新模型返回存储的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45627054/

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