将更新模型“推”回商店的正确方法是什么?

时间:2017-08-11 04:36:52

标签: ember.js ember-data ember-model

我正在尝试push更新后的model返回store。我试过几种方法仍然失败。

请帮助我了解如何在不更新model api的情况下将backend推回商店。

这是我的尝试:

    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

1 个答案:

答案 0 :(得分:2)

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以了解预期的结果格式。

阅读余烬指南models/pushing-records-into-the-store
阅读push API doc
阅读pushPayload doc -