Commit 484684de authored by Valentin Hervieu's avatar Valentin Hervieu

fix(): Change the watchers order to prevent unwanted model modifications

The order of watchers definition set the order in which their callback will be called when 2 watched
values are modified simultaneously. If the slider value is changed along with the precision options,
we want the value to be parsed with the new precision.

Closes #207
parent e9c097e0
...@@ -243,6 +243,12 @@ ...@@ -243,6 +243,12 @@
*/ */
this.initHasRun = false; this.initHasRun = false;
/**
* Internal flag to prevent watchers to be called when the sliders value are modified internally.
* @type {boolean}
*/
this.internalChange = false;
// Slider DOM elements wrapped in jqLite // Slider DOM elements wrapped in jqLite
this.fullBar = null; // The whole slider bar this.fullBar = null; // The whole slider bar
this.selBar = null; // Highlight between two handles this.selBar = null; // Highlight between two handles
...@@ -328,14 +334,26 @@ ...@@ -328,14 +334,26 @@
self.resetSlider(); self.resetSlider();
}); });
// Watchers // Watchers (order is important because in case of simultaneous change,
// watchers will be called in the same order)
this.scope.$watch('rzSliderOptions', function(newValue, oldValue) {
if (newValue === oldValue)
return;
self.applyOptions();
self.resetSlider();
}, true);
this.scope.$watch('rzSliderModel', function(newValue, oldValue) { this.scope.$watch('rzSliderModel', function(newValue, oldValue) {
if(self.internalChange)
return;
if (newValue === oldValue) if (newValue === oldValue)
return; return;
thrLow(); thrLow();
}); });
this.scope.$watch('rzSliderHigh', function(newValue, oldValue) { this.scope.$watch('rzSliderHigh', function(newValue, oldValue) {
if(self.internalChange)
return;
if (newValue === oldValue) if (newValue === oldValue)
return; return;
if (newValue != null) if (newValue != null)
...@@ -346,13 +364,6 @@ ...@@ -346,13 +364,6 @@
} }
}); });
this.scope.$watch('rzSliderOptions', function(newValue, oldValue) {
if (newValue === oldValue)
return;
self.applyOptions();
self.resetSlider();
}, true);
this.scope.$on('$destroy', function() { this.scope.$on('$destroy', function() {
self.unbindEvents(); self.unbindEvents();
angular.element($window).off('resize', calcDimFn); angular.element($window).off('resize', calcDimFn);
...@@ -1442,8 +1453,7 @@ ...@@ -1442,8 +1453,7 @@
this.scope.rzSliderHigh = newMaxValue; this.scope.rzSliderHigh = newMaxValue;
this.updateHandles('rzSliderModel', newMinOffset); this.updateHandles('rzSliderModel', newMinOffset);
this.updateHandles('rzSliderHigh', newMaxOffset); this.updateHandles('rzSliderHigh', newMaxOffset);
this.scope.$apply(); this.applyModel();
this.callOnChange();
}, },
/** /**
...@@ -1491,8 +1501,7 @@ ...@@ -1491,8 +1501,7 @@
} }
if (valueChanged) { if (valueChanged) {
this.scope.$apply(); this.applyModel();
this.callOnChange();
} }
return switched; return switched;
}, },
...@@ -1519,6 +1528,17 @@ ...@@ -1519,6 +1528,17 @@
} }
return eventNames; return eventNames;
},
/**
* Apply the model values using scope.$apply.
* We wrap it with the internalChange flag to avoid the watchers to be called
*/
applyModel: function() {
this.internalChange = true;
this.scope.$apply();
this.callOnChange();
this.internalChange = false;
} }
}; };
......
This diff is collapsed.
...@@ -247,6 +247,12 @@ ...@@ -247,6 +247,12 @@
*/ */
this.initHasRun = false; this.initHasRun = false;
/**
* Internal flag to prevent watchers to be called when the sliders value are modified internally.
* @type {boolean}
*/
this.internalChange = false;
// Slider DOM elements wrapped in jqLite // Slider DOM elements wrapped in jqLite
this.fullBar = null; // The whole slider bar this.fullBar = null; // The whole slider bar
this.selBar = null; // Highlight between two handles this.selBar = null; // Highlight between two handles
...@@ -332,14 +338,26 @@ ...@@ -332,14 +338,26 @@
self.resetSlider(); self.resetSlider();
}); });
// Watchers // Watchers (order is important because in case of simultaneous change,
// watchers will be called in the same order)
this.scope.$watch('rzSliderOptions', function(newValue, oldValue) {
if (newValue === oldValue)
return;
self.applyOptions();
self.resetSlider();
}, true);
this.scope.$watch('rzSliderModel', function(newValue, oldValue) { this.scope.$watch('rzSliderModel', function(newValue, oldValue) {
if(self.internalChange)
return;
if (newValue === oldValue) if (newValue === oldValue)
return; return;
thrLow(); thrLow();
}); });
this.scope.$watch('rzSliderHigh', function(newValue, oldValue) { this.scope.$watch('rzSliderHigh', function(newValue, oldValue) {
if(self.internalChange)
return;
if (newValue === oldValue) if (newValue === oldValue)
return; return;
if (newValue != null) if (newValue != null)
...@@ -350,13 +368,6 @@ ...@@ -350,13 +368,6 @@
} }
}); });
this.scope.$watch('rzSliderOptions', function(newValue, oldValue) {
if (newValue === oldValue)
return;
self.applyOptions();
self.resetSlider();
}, true);
this.scope.$on('$destroy', function() { this.scope.$on('$destroy', function() {
self.unbindEvents(); self.unbindEvents();
angular.element($window).off('resize', calcDimFn); angular.element($window).off('resize', calcDimFn);
...@@ -1446,8 +1457,7 @@ ...@@ -1446,8 +1457,7 @@
this.scope.rzSliderHigh = newMaxValue; this.scope.rzSliderHigh = newMaxValue;
this.updateHandles('rzSliderModel', newMinOffset); this.updateHandles('rzSliderModel', newMinOffset);
this.updateHandles('rzSliderHigh', newMaxOffset); this.updateHandles('rzSliderHigh', newMaxOffset);
this.scope.$apply(); this.applyModel();
this.callOnChange();
}, },
/** /**
...@@ -1495,8 +1505,7 @@ ...@@ -1495,8 +1505,7 @@
} }
if (valueChanged) { if (valueChanged) {
this.scope.$apply(); this.applyModel();
this.callOnChange();
} }
return switched; return switched;
}, },
...@@ -1523,6 +1532,17 @@ ...@@ -1523,6 +1532,17 @@
} }
return eventNames; return eventNames;
},
/**
* Apply the model values using scope.$apply.
* We wrap it with the internalChange flag to avoid the watchers to be called
*/
applyModel: function() {
this.internalChange = true;
this.scope.$apply();
this.callOnChange();
this.internalChange = false;
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment