Skip to content

Commit

Permalink
lookup JSONSerializer instance through store instead of manual instan…
Browse files Browse the repository at this point in the history
…tiation
  • Loading branch information
Stanley Stuart committed Jun 5, 2015
1 parent 0c5a3a3 commit 2a90daa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PromiseObject } from "ember-data/system/promise-proxies";
import JSONSerializer from "ember-data/serializers/json-serializer";

/**
@module ember-data
Expand Down Expand Up @@ -375,7 +374,7 @@ var Model = Ember.Object.extend(Ember.Evented, {
*/
toJSON: function(options) {
// container is for lazy transform lookups
var serializer = JSONSerializer.create({ container: this.container });
var serializer = this.store.serializerFor('-default');
var snapshot = this._internalModel.createSnapshot();

return serializer.serialize(snapshot, options);
Expand Down
36 changes: 36 additions & 0 deletions packages/ember-data/tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,39 @@ test("A subclass of DS.Model throws an error when calling create() directly", fu
Person.create();
}, /You should not call `create` on a model/, "Throws an error when calling create() on model");
});

test('toJSON looks up the JSONSerializer using the store instead of using JSONSerializer.create', function() {
var Person = DS.Model.extend({
posts: DS.hasMany('post')
});
var Post = DS.Model.extend({
person: DS.belongsTo('person')
});

var env = setupStore({
person: Person,
post: Post
});
var store = env.store;

var person, json, posts;
// Loading the person without explicitly
// loading its relationships seems to trigger the
// original bug where `this.store` was not
// present on the serializer due to using .create
// instead of `store.serializerFor`.
run(() => {
person = store.push('person', {
id: 1
});
});
var errorThrown = false;
try {
json = run(person, 'toJSON');
} catch (e) {
errorThrown = true;
}

ok(!errorThrown, 'error not thrown due to missing store');
deepEqual(json, {});
});

0 comments on commit 2a90daa

Please sign in to comment.