@@ -69,7 +69,7 @@ var ThreeModel = widgets.WidgetModel.extend({
69
69
this . listenTo ( curValue , 'childchange' , this . onChildChanged ) ;
70
70
}
71
71
72
- // make sure to un/ hook listeners when child points to new object
72
+ // make sure to (un) hook listeners when child points to new object
73
73
this . on ( 'change:' + propName , function ( model , value , options ) {
74
74
var prevModel = this . previous ( propName ) ;
75
75
var currModel = value ;
@@ -93,7 +93,7 @@ var ThreeModel = widgets.WidgetModel.extend({
93
93
this . listenTo ( childModel , 'childchange' , this . onChildChanged ) ;
94
94
} , this ) ;
95
95
96
- // make sure to un/ hook listeners when array changes
96
+ // make sure to (un) hook listeners when array changes
97
97
this . on ( 'change:' + propName , function ( model , value , options ) {
98
98
var prevArr = this . previous ( propName ) || [ ] ;
99
99
var currArr = value || [ ] ;
@@ -111,7 +111,42 @@ var ThreeModel = widgets.WidgetModel.extend({
111
111
} , this ) ;
112
112
} , this ) ;
113
113
114
- // TODO: handle dicts of children via this.three_dict_properties
114
+ // Handle changes in three instance dict props
115
+ this . three_dict_properties . forEach ( function ( propName ) {
116
+
117
+ var currDict = this . get ( propName ) || { } ;
118
+
119
+ // listen to current values in dict
120
+ var childModel ;
121
+ Object . keys ( currDict ) . forEach ( function ( childModelKey ) {
122
+ childModel = currDict [ childModelKey ] ;
123
+ this . listenTo ( childModel , 'change' , this . onChildChanged ) ;
124
+ this . listenTo ( childModel , 'childchange' , this . onChildChanged ) ;
125
+ } , this ) ;
126
+
127
+ // make sure to (un)hook listeners when dict changes
128
+ this . on ( 'change:' + propName , function ( model , value , options ) {
129
+ var prevDict = this . previous ( propName ) || { } ;
130
+ var currDict = value || { } ;
131
+
132
+ var prevKeys = Object . keys ( prevDict ) ;
133
+ var currKeys = Object . keys ( currDict ) ;
134
+
135
+ var added = _ . difference ( currKeys , prevKeys ) ;
136
+ var removed = _ . difference ( prevKeys , currKeys ) ;
137
+
138
+ added . forEach ( function ( childModelKey ) {
139
+ childModel = currDict [ childModelKey ] ;
140
+ this . listenTo ( childModel , 'change' , this . onChildChanged ) ;
141
+ this . listenTo ( childModel , 'childchange' , this . onChildChanged ) ;
142
+ } , this ) ;
143
+ removed . forEach ( function ( childModelKey ) {
144
+ childModel = prevDict [ childModelKey ] ;
145
+ this . stopListening ( childModel ) ;
146
+ } , this ) ;
147
+ } , this ) ;
148
+
149
+ } , this ) ;
115
150
116
151
this . on ( 'change' , this . onChange , this ) ;
117
152
this . on ( 'msg:custom' , this . onCustomMessage , this ) ;
0 commit comments