Skip to content

Commit dae8e71

Browse files
committed
Ensure we corrently listen for array/instance attrs
1 parent a439b33 commit dae8e71

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

js/src/_base/Three.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,58 @@ function listenNested(model, propNames, callback) {
2424
propNames.forEach(function(propName) {
2525
// listen to current values in array
2626
var curr = model.get(propName) || [];
27-
// ignore ThreeModels in order to support properties
28-
// that are either an instance, or a sequence of instances:
27+
// support properties that are either an instance, or a
28+
// sequence of instances:
2929
if (curr instanceof ThreeModel) {
30-
return;
30+
model.listenTo(curr, 'change', callback);
31+
model.listenTo(curr, 'childchange', callback);
32+
} else {
33+
utils.childModelsNested(curr).forEach(function(childModel) {
34+
model.listenTo(childModel, 'change', callback);
35+
model.listenTo(childModel, 'childchange', callback);
36+
});
3137
}
32-
utils.childModelsNested(curr).forEach(function(childModel) {
33-
model.listenTo(childModel, 'change', callback);
34-
model.listenTo(childModel, 'childchange', callback);
35-
});
3638

3739
// make sure to (un)hook listeners when array changes
3840
model.on('change:' + propName, function(model, value) {
3941
var prev = model.previous(propName) || [];
4042
var curr = value || [];
4143

42-
var diff = utils.nestedDiff(curr, prev);
44+
// Check for instance values:
45+
if (prev instanceof ThreeModel) {
46+
model.stopListening(prev);
47+
}
48+
if (curr instanceof ThreeModel) {
49+
model.listenTo(curr, 'change', callback);
50+
model.listenTo(curr, 'childchange', callback);
51+
}
52+
// Done if both are instance values:
53+
if (prev instanceof ThreeModel && curr instanceof ThreeModel) {
54+
return;
55+
}
56+
if (prev instanceof ThreeModel) {
57+
// Implies curr is array
58+
utils.childModelsNested(curr).forEach(function(childModel) {
59+
model.listenTo(childModel, 'change', callback);
60+
model.listenTo(childModel, 'childchange', callback);
61+
});
62+
} else if (curr instanceof ThreeModel) {
63+
// Implies prev is array
64+
utils.childModelsNested(prev).forEach(function(childModel) {
65+
model.stopListening(childModel);
66+
});
67+
} else {
68+
// Both are arrays
69+
var diff = utils.nestedDiff(curr, prev);
4370

44-
diff.added.forEach(function(childModel) {
45-
model.listenTo(childModel, 'change', callback);
46-
model.listenTo(childModel, 'childchange', callback);
47-
});
48-
diff.removed.forEach(function(childModel) {
49-
model.stopListening(childModel);
50-
});
71+
diff.added.forEach(function(childModel) {
72+
model.listenTo(childModel, 'change', callback);
73+
model.listenTo(childModel, 'childchange', callback);
74+
});
75+
diff.removed.forEach(function(childModel) {
76+
model.stopListening(childModel);
77+
});
78+
}
5179
});
5280
});
5381
}

0 commit comments

Comments
 (0)