Skip to content

Commit

Permalink
Merge pull request #3148 from yratanov/feature/snapshot-serialize
Browse files Browse the repository at this point in the history
add Snapshot#serialize method
  • Loading branch information
igorT committed Jun 3, 2015
2 parents d6c46d8 + 50c4195 commit 3897ea1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/ember-data/lib/system/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,15 @@ Snapshot.prototype = {
return get(this.record, keyName);
},

/**
@method serialize
@param {Object} options
@return {Object} an object whose values are primitive JSON values only
*/
serialize: function(options) {
return this.record.store.serializerFor(this.modelName).serialize(this, options);
},

/**
@method unknownProperty
@param {String} keyName
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ Store = Service.extend({
*/
serialize: function(record, options) {
var snapshot = record._internalModel.createSnapshot();
return this.serializerFor(snapshot.modelName).serialize(snapshot, options);
return snapshot.serialize(options);
},

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/ember-data/tests/integration/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,20 @@ test("snapshot.get() proxies property to record unless identified as id, attribu
});
});

test("snapshot.serialize() serializes itself", function() {
expect(2);

run(function() {
var post = env.store.push('post', { id: 1, title: 'Hello World' });
var snapshot = post._createSnapshot();

post.set('title', 'New Title');

deepEqual(snapshot.serialize(), { author: undefined, title: 'Hello World' }, 'shapshot serializes correctly');
deepEqual(snapshot.serialize({ includeId: true }), { id: "1", author: undefined, title: 'Hello World' }, 'serialize takes options')
});
});

test('snapshot.typeKey is deprecated', function() {
expect(1);

Expand Down

0 comments on commit 3897ea1

Please sign in to comment.