Ember.js 将更新后的模型“推送”回商店的正确方法是什么?
我正在尝试Ember.js 将更新后的模型“推送”回商店的正确方法是什么?,ember.js,ember-data,ember-model,Ember.js,Ember Data,Ember Model,我正在尝试将更新的模型推回到存储。我尝试了几种方法,但仍然失败了 请帮助我了解如何在不更新后端api的情况下将模型推回存储区 以下是我的尝试: import Ember from 'ember'; export default Ember.Route.extend({ model: function(params) { if(this.store.hasRecordForId('card-list', params.id)){
将更新的模型推回到存储
。我尝试了几种方法,但仍然失败了
请帮助我了解如何在不更新后端
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);
请参阅了解预期结果格式
阅读余烬指南
阅读
阅读-只需确保更新的模型
为预期格式。确定会尝试并返回给您。当我按下model
按钮时,仍然id
需要手动添加吗?是。存储就像客户端的数据库,它需要id来唯一地标识记录。没问题。如果它已经存在,那么它将更新记录。如果没有,它将添加新的记录非常感谢你。它起作用了。请不要停下来帮助别人。。我们只有很少的资源像你一样得到帮助!!!真是太好了!。