aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVojta Jina2012-03-09 17:19:48 -0800
committerVojta Jina2012-03-09 17:33:22 -0800
commit716dd5f3f90f853713706aa3b404822fef18ef93 (patch)
treeeb30e28a834c70fb1d799ad2cba0014171445ae7
parent83314913e717f18cdb9a817e67c24651930ebe7e (diff)
downloadangular.js-716dd5f3f90f853713706aa3b404822fef18ef93.tar.bz2
refactor(forms): Remove touch() method
-rw-r--r--src/directive/input.js63
-rw-r--r--src/directive/select.js8
-rw-r--r--test/directive/formSpec.js2
-rw-r--r--test/directive/inputSpec.js36
4 files changed, 32 insertions, 77 deletions
diff --git a/src/directive/input.js b/src/directive/input.js
index 0552be64..16d8f6c8 100644
--- a/src/directive/input.js
+++ b/src/directive/input.js
@@ -369,16 +369,9 @@ function isEmpty(value) {
function textInputType(scope, element, attr, ctrl) {
element.bind('blur', function() {
- var touched = ctrl.touch(),
- value = trim(element.val());
-
- if (ctrl.viewValue !== value) {
- scope.$apply(function() {
- ctrl.setViewValue(value);
- });
- } else if (touched) {
- scope.$apply();
- }
+ scope.$apply(function() {
+ ctrl.setViewValue(trim(element.val()));
+ });
});
ctrl.render = function() {
@@ -558,7 +551,6 @@ function radioInputType(scope, element, attr, ctrl) {
element.bind('click', function() {
if (element[0].checked) {
scope.$apply(function() {
- ctrl.touch();
ctrl.setViewValue(attr.value);
});
};
@@ -579,7 +571,6 @@ function checkboxInputType(scope, element, attr, ctrl) {
element.bind('click', function() {
scope.$apply(function() {
- ctrl.touch();
ctrl.setViewValue(element[0].checked);
});
});
@@ -767,34 +758,6 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel',
/**
* @ngdoc function
- * @name angular.module.ng.$compileProvider.directive.ng-model.NgModelController#touch
- * @methodOf angular.module.ng.$compileProvider.directive.ng-model.NgModelController
- *
- * @return {boolean} Whether it did change state.
- *
- * @description
- * This method should be called from within a DOM event handler.
- * For example {@link angular.module.ng.$compileProvider.directive.input input} or
- * {@link angular.module.ng.$compileProvider.directive.select select} directives call it.
- *
- * It changes state to `dirty` and emits `$viewTouch` event if the state was `pristine` before.
- */
- this.touch = function() {
- if (this.dirty) return false;
-
- this.dirty = true;
- this.pristine = false;
- try {
- $scope.$emit('$viewTouch');
- } catch (e) {
- $exceptionHandler(e);
- }
- return true;
- };
-
-
- /**
- * @ngdoc function
* @name angular.module.ng.$compileProvider.directive.ng-model.NgModelController#setValidity
* @methodOf angular.module.ng.$compileProvider.directive.ng-model.NgModelController
*
@@ -848,6 +811,13 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel',
this.setViewValue = function(value) {
this.viewValue = value;
+ // change to dirty
+ if (this.pristine) {
+ this.dirty = true;
+ this.pristine = false;
+ $scope.$emit('$viewTouch', this);
+ }
+
forEach(this.parsers, function(fn) {
value = fn(value);
});
@@ -1037,16 +1007,9 @@ var ngModelInstantDirective = ['$browser', function($browser) {
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
var handler = function() {
- var touched = ctrl.touch(),
- value = trim(element.val());
-
- if (ctrl.viewValue !== value) {
- scope.$apply(function() {
- ctrl.setViewValue(value);
- });
- } else if (touched) {
- scope.$apply();
- }
+ scope.$apply(function() {
+ ctrl.setViewValue(trim(element.val()));
+ });
};
var timeout;
diff --git a/src/directive/select.js b/src/directive/select.js
index 3cd1b99c..0ab35d1c 100644
--- a/src/directive/select.js
+++ b/src/directive/select.js
@@ -167,7 +167,6 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
selectElement.bind('change', function() {
scope.$apply(function() {
- ctrl.touch();
ctrl.setViewValue(selectElement.val());
});
});
@@ -189,7 +188,6 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
array.push(option.value);
}
});
- ctrl.touch();
ctrl.setViewValue(array);
});
});
@@ -267,11 +265,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
value = valueFn(scope, locals);
}
}
- ctrl.touch();
-
- if (ctrl.viewValue !== value) {
- ctrl.setViewValue(value);
- }
+ ctrl.setViewValue(value);
});
});
diff --git a/test/directive/formSpec.js b/test/directive/formSpec.js
index 6559da5d..45af04a4 100644
--- a/test/directive/formSpec.js
+++ b/test/directive/formSpec.js
@@ -197,7 +197,7 @@ describe('form', function() {
it('should have ng-pristine/ng-dirty css class', function() {
expect(doc).toBePristine();
- widget.touch();
+ widget.setViewValue('');
scope.$apply();
expect(doc).toBeDirty();
});
diff --git a/test/directive/inputSpec.js b/test/directive/inputSpec.js
index e7c40b0a..6298fc64 100644
--- a/test/directive/inputSpec.js
+++ b/test/directive/inputSpec.js
@@ -34,25 +34,6 @@ describe('NgModelController', function() {
});
- describe('touch', function() {
- it('should only fire $viewTouch when pristine', function() {
- var spy = jasmine.createSpy('$viewTouch');
- scope.$on('$viewTouch', spy);
-
- ctrl.touch();
- expect(ctrl.pristine).toBe(false);
- expect(ctrl.dirty).toBe(true);
- expect(spy).toHaveBeenCalledOnce();
-
- spy.reset();
- ctrl.touch();
- expect(ctrl.pristine).toBe(false);
- expect(ctrl.dirty).toBe(true);
- expect(spy).not.toHaveBeenCalled();
- });
- });
-
-
describe('setValidity', function() {
it('should emit $invalid only when $valid', function() {
@@ -150,6 +131,23 @@ describe('NgModelController', function() {
ctrl.setViewValue('val');
expect(spy).not.toHaveBeenCalled();
});
+
+
+ it('should only fire $viewTouch when pristine', function() {
+ var spy = jasmine.createSpy('$viewTouch');
+ scope.$on('$viewTouch', spy);
+
+ ctrl.setViewValue('');
+ expect(ctrl.pristine).toBe(false);
+ expect(ctrl.dirty).toBe(true);
+ expect(spy).toHaveBeenCalledOnce();
+
+ spy.reset();
+ ctrl.setViewValue('');
+ expect(ctrl.pristine).toBe(false);
+ expect(ctrl.dirty).toBe(true);
+ expect(spy).not.toHaveBeenCalled();
+ });
});