Skip to content

Commit aa8f553

Browse files
committed
Bug #170 Internal model not updating when changed programatically
1 parent 83d0a8a commit aa8f553

File tree

7 files changed

+40
-20
lines changed

7 files changed

+40
-20
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ rules:
188188
max-statements-per-line: off
189189
max-statements:
190190
- error
191-
- 30
191+
- 50
192192
multiline-ternary: off
193193
new-cap: off
194194
new-parens: off

examples/01-simple.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ <h3>Examples</h3>
226226
<label class="control-label">Color</label>
227227
<button ng-click="open()">Show</button>
228228
<button ng-click="close()">Hide</button>
229+
<button ng-click="setValue('#FF0000')">Set value to #FF0000</button>
229230
<color-picker
230231
ng-model="color"
231232
options="options"

examples/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@ angular
6060
$scope.close = function() {
6161
$scope.api.close();
6262
};
63+
64+
$scope.setValue = function(value) {
65+
$scope.color = value;
66+
};
6367
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"load-grunt-tasks": "^3.5.0",
2929
"moment": "^2.14.1",
3030
"protractor": "^5.0.0",
31-
"rollup": "^0.47.0",
31+
"rollup": "^0.50.0",
3232
"rollup-plugin-babel": "^3.0.0",
3333
"rollup-plugin-uglify": "^2.0.0",
3434
"time-grunt": "^1.3.0"

src/scripts/controller.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ export default class AngularColorPickerController {
3434
this.ngModelOptions = this.$scope.control[0].$options.$$options;
3535
}
3636

37-
this.internalNgModel = this.ngModelOptions.getterSetter ? this.ngModel() : this.ngModel;
38-
3937
// browser variables
4038
this.chrome = Boolean(window.chrome);
4139
let _android_version = window.navigator.userAgent.match(/Android\s([0-9\.]*)/i);
@@ -98,7 +96,7 @@ export default class AngularColorPickerController {
9896

9997
// ngModel
10098

101-
this.$scope.$watch('AngularColorPickerController.internalNgModel', this.watchNgModel.bind(this));
99+
this.$scope.$watch('AngularColorPickerController.internalNgModel', this.watchInternalNgModel.bind(this));
102100
this.$scope.$watch('AngularColorPickerController.ngModel', this.watchNgModel.bind(this));
103101

104102
// options
@@ -174,6 +172,16 @@ export default class AngularColorPickerController {
174172
});
175173
}
176174

175+
watchInternalNgModel(newValue, oldValue) {
176+
// the mouse is still moving so don't do anything yet
177+
if (this.colorMouse) {
178+
return;
179+
}
180+
181+
// calculate and set color values
182+
this.watchNgModelSet(newValue);
183+
}
184+
177185
/** Triggered on change to internal or external ngModel value */
178186
watchNgModel(newValue, oldValue) {
179187
// set initial value if not already set
@@ -184,6 +192,9 @@ export default class AngularColorPickerController {
184192
// sets the field to pristine or dirty for angular
185193
this.checkDirty(newValue);
186194

195+
// update the internal model from external model
196+
this.internalNgModel = this.ngModelOptions.getterSetter ? this.ngModel() : this.ngModel;
197+
187198
// the mouse is still moving so don't do anything yet
188199
if (this.colorMouse) {
189200
return;

test/page-object.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
class Page {
33
constructor() {
44
this.url = 'http://localhost:8000/test/test.html';
5-
// this.url = 'http://localhost/angular-color-picker/test/test.html';
65
this.body = element(by.css('body'));
76

87
// attribute fields
@@ -89,6 +88,14 @@ class Page {
8988
blurColorPicker() {
9089
browser.driver.executeScript('document.activeElement.blur()');
9190
}
91+
92+
getNgModel() {
93+
return this.color_picker.evaluate('color');
94+
}
95+
96+
setNgModel(value) {
97+
return this.color_picker.evaluate(`color = '${value}'`);
98+
}
9299
}
93100

94101
module.exports = new Page();

test/test.html

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
<h1>AngularJS Color Picker</h1>
1717
<p>An AngularJS directive for a color picker. No requirement on jQuery.</p>
1818
<form class="col-sm-12">
19+
<div class="row">
20+
<label class="control-label">Color</label>
21+
<button ng-click="open()">Show</button>
22+
<button ng-click="close()">Hide</button>
23+
<color-picker
24+
ng-model="color"
25+
options="options"
26+
api="api"
27+
event-api="eventApi"
28+
></color-picker>
29+
</div>
1930

2031
<h3>Color</h3>
2132

@@ -210,20 +221,6 @@ <h3>Buttons</h3>
210221
<label class="control-label">Reset class (reset.class) - The class of the reset button - ""</label>
211222
<input type="text" ng-model="options.reset.class" placeholder="Class" class="form-control" />
212223
</div>
213-
214-
<h3>Examples</h3>
215-
216-
<div class="row">
217-
<label class="control-label">Color</label>
218-
<button ng-click="open()">Show</button>
219-
<button ng-click="close()">Hide</button>
220-
<color-picker
221-
ng-model="color"
222-
options="options"
223-
api="api"
224-
event-api="eventApi"
225-
></color-picker>
226-
</div>
227224
</form>
228225
</div>
229226
</body>

0 commit comments

Comments
 (0)