将更新后的模型“推送”回存储的正确方法是什么?
What is the correct way to `push` a updated model back to store?
我正在尝试 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 -
我正在尝试 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 -