更新并将模型保存到存储而不连接后端后会出现错误



我正在尝试更新我的模式,只是存储而不连接后端。 成功完成后,我将使用它的 id 检索更新的模型。

为此,我正在尝试将我的模型推送到存储(更新存储中的现有模型(,但出现错误。

有人打我吗?

这是我的代码:

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 );//getting
}
},
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");
console.log( "model would be:-" , JSON.parse(JSON.stringify(model)) );
this.store.push(JSON.parse(model));//updating without connect to backend,but throws error
console.log( "store data", this.store.peekRecord('card-list', modelId ) )
this.transitionTo('cs2i.balance.balanceReview', {id:modelId});
}
}
});

这取决于要使用的序列化程序。我假设您使用的是默认的 JSONSerializer。如果是这种情况,下面介绍了如何将数据推送到存储中。

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 );//getting
}
},
actions:{
formValidateBeforeNext:function(){
var modelData = { //creating a sample data for testing purpose. You can fetch the data as above from your example
id:1,
type:"card-list",
attributes:{
oneTimeFee:1000,
testPayment:2000
}
}
console.log( "model would be:-" , JSON.parse(JSON.stringify(modelData)) );
this.store.push({
data: modelData
});//updating without connect to backend,but throws error
console.log( "store data", this.store.peekRecord('card-list', 1 ) )
this.transitionTo('cs2i.balance.balanceReview', {id:modelId});
}
}
});

即使是 RestSerializer,您也只需使用 this.store.pushPayload 并使用模型名称("cardList"(作为键而不是数据。

希望这有帮助。

最新更新