Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add pushPayload method to store #1309

Merged
merged 1 commit into from

2 participants

Alex Speller Yehuda Katz
Alex Speller

Addresses #705

Yehuda Katz
Owner

Probably the biggest issue with this is that it assumes the existence of pushPayload, which only exists on RESTAdapter.

That said, I think I could live with this, if it was called pushPayload. I need to talk to Tom about it. In the meantime, can you rename it?

Alex Speller

@wycats done, that is obviously a better name and I don't know why I didn't use it in the first place.

Yehuda Katz
Owner

I'm going to merge this, but I would like it if you opened a new PR with additional documentation that explained the expected payload format for the REST Serializer.

Yehuda Katz wycats merged commit 995b366 into from
Alex Speller

Do you mean in the YUI doc above the method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 18, 2013
  1. Alex Speller
This page is out of date. Refresh to see the latest.
38 packages/ember-data/lib/system/store.js
View
@@ -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);
26 packages/ember-data/tests/unit/store/push_test.js
View
@@ -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", {
@@ -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;
@@ -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() {
@@ -140,3 +146,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");
+});
Something went wrong with that request. Please try again.