What is the correct way to `push` a updated model back to store?

879 Views Asked by At

I am trying to push the updated model back to store. i tried with couple of ways still getting failed.

Please help me to understand to push the model back to store without updating backend api.

here is my try:

    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});

        }
      }
    });

What is wrong here? what is the correct way to put back the mode with updates?

UPDATE:Error added

Expected an object in the 'data' property in a call to 'push' for undefined, but was instance
Error
1

There are 1 best solutions below

10
Ember Freak On BEST ANSWER

push method will expect the data in the expected format. for eg, if you are using the JSONAPI. the below is the expected one.

store.push({
  data: {
    // primary data for single record of type `Person`
    id: '1',
    type: 'person',
    attributes: {
      firstName: 'Daniel',
      lastName: 'Kmak'
    }
  }
});

You can convert json payload into the expected form by doing so,

store.push(store.normalize('person', data));

If you are having raw JSON data, then you can try pushPayload.

this.get('store').pushPayload('card-list',data);

Refer EmberData Model Maker to know expected result format.

Read ember guides models/pushing-records-into-the-store
Read push API doc
Read pushPayload doc -