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

时间:2017-08-10 12:44:00

标签: json ember.js

我试图仅在没有连接后端的情况下更新我的模式。成功完成后,我将使用它的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});

    }
  }
});

1 个答案:

答案 0 :(得分:1)

这取决于您要使用的序列化程序。我假设你正在使用默认的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”)作为键而不是数据。

希望这有帮助。