Help! store.pushPayload is driving me insane


#1

Do you have any ideas why this code isn’t working?

App.js

App.ApplicationSerializer = DS.ActiveModelSerializer;

Room.js

App.Room = DS.Model.extend({
  name         : DS.attr(),
  url          : DS.attr(),
  created_at   : DS.attr(),
  updated_at   : DS.attr(),
  messages: DS.hasMany('message',{async:true})
});
App.Room.FIXTURES = [{
  id: 1,
  name: 'Lobby',
  url: 'lobby',
  messages: [1,2]
}];

Router.js

App.RoomRoute = Ember.Route.extend({
  debug:function(){
    this.store.find('room',1).then(function(record){
      console.log('*==================*');
      console.log('DEBUG');
      console.log('*==================*');
      console.log(record.toJSON());
    });
  },
  beforeModel: function(transition) {
    this.debug();
    var myRoom = {"room":[{"id":1,"url":"kitchen","name":"Kitchen","message_ids":[]}],"message":[]};
    store.pushPayload('room', myRoom);
    this.debug();
  }
});

When I run this payload, nothing changes. The “room” with id 1 set in the fixture hasn’t changed.

Plesae help me stay sane :slight_smile:


#2

Just a wild guess, but I think the root keys of your payload should be pluralized.


#3

Either that or he should remove the square brackets to push a single item.


#4

like this

var myRoom = {"rooms": [ {"id":1,"url":"kitchen","name":"Kitchen","message_ids":[]} ]};
store.pushPayload('room', myRoom);

or just push a single record

var myRoom = {"room": {"id":1,"url":"kitchen","name":"Kitchen","message_ids":[]}};
store.push('room', myRoom);

#5

I see, is it a rule to pluralize them like that?


#6

Also, push payload will only make local changes.

You should use store.find to get the model. Make changes to the model then use model.save


#7

@billybonks I guess it depends on the adapter? The adapter can implement pushPayload? Or can it?


#8

it would depend on the store, if you customised the Store,pushPayload then you could get that behaviour, but I wouldn’t suggest going against the grain unless you can give a good reason for it :smile:


#9

How its working for me

var user = {id: 1, email: "Ringo@star.com", firstname: "Ringo"};
var normalizedUser = @store.serializerFor('user').normalize(App.User, user);  
//normaly 'user', instead of App.User, but that doesnt work for me
@store.push('user', normalizedUser)
@store.all('user').get('length') // returns 1