Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Snapshot#serialize method #3148

Merged
merged 1 commit into from
Jun 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -598,6 +598,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