Skip to content

Commit

Permalink
Merge pull request #1309 from alexspeller/add-pushRaw-method
Browse files Browse the repository at this point in the history
Add pushPayload method to store
  • Loading branch information
wycats committed Sep 19, 2013
2 parents a29dbcf + aa8200a commit 995b366
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
38 changes: 38 additions & 0 deletions packages/ember-data/lib/system/store.js
Expand Up @@ -1104,6 +1104,44 @@ DS.Store = Ember.Object.extend(DS._Mappable, {
return this.recordForId(type, data.id);
},

/**
Push some raw data into the store.
The data will be automatically deserialized using the
serializer for the `type` param.
This method can be used both to push in brand new
records, as well as to update existing records.
You can push in more than one type of object at once.
All objects should be in the format expected by the
serializer.
```js
App.ApplicationSerializer = DS.ActiveModelSerializer;
var pushData = {
posts: [
{id: 1, post_title: "Great post", comment_ids: [2]}
],
comments: [
{id: 2, comment_body: "Insightful comment"}
]
}
store.pushPayload('post', pushData);
```
@method push
@param {String} type
@param {Object} payload
*/

pushPayload: function (type, payload) {
var serializer = this.serializerFor(type);
serializer.pushPayload(this, payload);
},

update: function(type, data) {
Ember.assert("You must include an `id` in a hash passed to `update`", data.id != null);

Expand Down
26 changes: 25 additions & 1 deletion packages/ember-data/tests/unit/store/push_test.js
@@ -1,4 +1,4 @@
var env, store, Person, PhoneNumber;
var env, store, Person, PhoneNumber, Post;
var attr = DS.attr, hasMany = DS.hasMany, belongsTo = DS.belongsTo;

module("unit/store/push - DS.Store#push", {
Expand All @@ -14,6 +14,10 @@ module("unit/store/push - DS.Store#push", {
person: belongsTo('person')
});

Post = DS.Model.extend({
postTitle: attr('string')
});

env = setupStore();

store = env.store;
Expand All @@ -24,7 +28,9 @@ module("unit/store/push - DS.Store#push", {

env.container.register('model:person', Person);
env.container.register('model:phone-number', PhoneNumber);
env.container.register('model:post', Post);
env.container.register('serializer:_default', DefaultSerializer);
env.container.register('serializer:post', DS.ActiveModelSerializer);
},

teardown: function() {
Expand Down Expand Up @@ -154,3 +160,21 @@ test("Calling push with a normalized hash containing IDs of related records retu
}]);
}));
});

test("Calling pushPayload allows pushing raw JSON", function () {
store.pushPayload('post', {posts: [{
id: '1',
post_title: "Ember rocks"
}]});

var post = store.getById('post', 1);

equal(post.get('postTitle'), "Ember rocks", "you can push raw JSON into the store");

store.pushPayload('post', {posts: [{
id: '1',
post_title: "Ember rocks (updated)"
}]});

equal(post.get('postTitle'), "Ember rocks (updated)", "You can update data in the store");
});

1 comment on commit 995b366

@hajee
Copy link

@hajee hajee commented on 995b366 Sep 26, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PushPayload now only works if the payload contains an array. The name pushPayload suggests to me any valid payload no matter if it is a single record, an array, or a payload with several items te be side loaded. Are there any plans to make it work for these cases as well?

Please sign in to comment.