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

After update and saving the model to store without connecting backend gives error

我正在尝试更新我的模式,仅存储而不连接后端。成功完成后,我将使用它的 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") 作为键而不是数据。

希望对您有所帮助。