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

Prevent duplicate handling of sideloaded values #669

Merged
merged 1 commit into from Jan 31, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/ember-data/lib/serializers/json_serializer.js
Expand Up @@ -218,14 +218,15 @@ DS.JSONSerializer = DS.Serializer.extend({
},

sideloadRelationships: function(loader, type, json, prop, loaded) {
if (loaded[prop]) { return; }
loaded[prop] = true;

get(type, 'relationshipsByName').forEach(function(key, meta) {
key = meta.key || key;
if (meta.kind === 'belongsTo') {
key = this.pluralize(key);
}
if (json[key] && !loaded[key]) {
if (json[key]) {
this.sideloadRelationships(loader, meta.type, json, key, loaded);
}
}, this);
Expand Down
57 changes: 57 additions & 0 deletions packages/ember-data/tests/integration/json_serialization_test.js
Expand Up @@ -170,3 +170,60 @@ test("the default addRelationships calls addHasMany", function() {

serializer.serialize(post);
});

test("loadValue should be called once per sideloaded type", function() {
var payload, loader, K = Ember.K, loadedTypes = [], App = Ember.Namespace.create({
toString: function() { return "App"; }
});

App.Fan = DS.Model.extend({
name: DS.attr('string')
});

App.Player = DS.Model.extend({
name: DS.attr('string'),
fans: DS.hasMany(App.Fan)
});

App.Coach = DS.Model.extend({
name: DS.attr('string'),
fans: DS.hasMany(App.Fan),
players: DS.hasMany(App.Player)
});

serializer.configure(App.Coach, {
sideloadAs: 'coaches'
});

App.Team = DS.Model.extend({
name: DS.attr('string'),
mascots: DS.hasMany(App.Coach),
fans: DS.hasMany(App.Fan),
players: DS.hasMany(App.Player)
});

payload = {
coaches: [{
id: 1, name: "Peter Wagenet", fans: [ 1 ], players: [ 1 ]
}],
fans: [{
id: 1, name: "Yehuda Katz"
}],
players: [{
id: 1, name: "Tom Dale", fans: [ 1 ]
}],
team: {
id: 1, name: "49ers", fans: [ 1 ], players: [ 1 ], coaches: [ 1 ]
}
};

loader = { load: K, loadMany: K, prematerialize: K, sinceForType: K };

serializer.loadValue = function(store, type, value) {
loadedTypes.push(type);
};

serializer.extract(loader, payload, App.Team);

equal(loadedTypes.length, 3, "Loaded: " + loadedTypes.join(", "));
});