Skip to content

Loading…

Prevent duplicate handling of sideloaded values #669

Merged
merged 1 commit into from

3 participants

@raycohen

No description provided.

@stefanpenner
Ember.js member

:+1: @wagenet lets get this rolled in...

@wycats wycats merged commit 4644999 into emberjs:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 28, 2013
  1. @raycohen
This page is out of date. Refresh to see the latest.
View
3 packages/ember-data/lib/serializers/json_serializer.js
@@ -218,6 +218,7 @@ 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) {
@@ -225,7 +226,7 @@ DS.JSONSerializer = DS.Serializer.extend({
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);
View
57 packages/ember-data/tests/integration/json_serialization_test.js
@@ -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(", "));
+});
Something went wrong with that request. Please try again.