From c75202d5d7ecabd01366f2198e0c0c3b5c087e59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matias=20Niemel=C3=A4?= Date: Wed, 12 Feb 2014 11:32:06 -0500 Subject: [PATCH] feat(forms): provide support for reseting forms, fieldsets and models --- lib/directive/ng_control.dart | 5 +++++ lib/directive/ng_model.dart | 10 ++++++++++ test/directive/ng_form_spec.dart | 31 +++++++++++++++++++++++++++++++ test/directive/ng_model_spec.dart | 26 ++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/lib/directive/ng_control.dart b/lib/directive/ng_control.dart index a9042c75d..1d787b5f9 100644 --- a/lib/directive/ng_control.dart +++ b/lib/directive/ng_control.dart @@ -32,6 +32,10 @@ abstract class NgControl implements NgDetachAware { } } + reset() { + _scope.$broadcast('resetNgModel'); + } + get name => _name; set name(value) { _name = value; @@ -168,5 +172,6 @@ class NgNullControl implements NgControl { get invalid => null; set invalid(value) {} + reset() => null; detach() => null; } diff --git a/lib/directive/ng_model.dart b/lib/directive/ng_model.dart index 892ae973f..b6a7b67b7 100644 --- a/lib/directive/ng_model.dart +++ b/lib/directive/ng_model.dart @@ -15,6 +15,7 @@ class NgModel extends NgControl { BoundGetter getter = ([_]) => null; BoundSetter setter = (_, [__]) => null; + var _lastValue; String _exp; final _validators = []; @@ -26,6 +27,11 @@ class NgModel extends NgControl { super(scope, element, injector) { _exp = 'ng-model=${attrs["ng-model"]}'; watchCollection = false; + scope.$on('resetNgModel', reset); + } + + reset() { + modelValue = _lastValue; } @NgAttr('name') @@ -51,6 +57,10 @@ class NgModel extends NgControl { set model(BoundExpression boundExpression) { getter = boundExpression; setter = boundExpression.assign; + + _scope.$evalAsync((value) { + _lastValue = modelValue; + }); } // TODO(misko): right now viewValue and modelValue are the same, diff --git a/test/directive/ng_form_spec.dart b/test/directive/ng_form_spec.dart index f48ee64f2..6b3da4ab4 100644 --- a/test/directive/ng_form_spec.dart +++ b/test/directive/ng_form_spec.dart @@ -306,6 +306,37 @@ describe('form', () { })); }); + describe('reset()', () { + it('should reset the model value to its original state', inject((TestBed _) { + _.compile('
' + + ' ' + + '
'); + _.rootScope.$apply('myModel = "animal"'); + + NgForm form = _.rootScope['superForm']; + + Probe probe = _.rootScope.i; + var model = probe.directive(NgModel); + + expect(_.rootScope.myModel).toEqual('animal'); + expect(model.modelValue).toEqual('animal'); + expect(model.viewValue).toEqual('animal'); + + _.rootScope.$apply('myModel = "man"'); + + expect(_.rootScope.myModel).toEqual('man'); + expect(model.modelValue).toEqual('man'); + expect(model.viewValue).toEqual('man'); + + form.reset(); + _.rootScope.$apply(); + + expect(_.rootScope.myModel).toEqual('animal'); + expect(model.modelValue).toEqual('animal'); + expect(model.viewValue).toEqual('animal'); + })); + }); + describe('regression tests: form', () { it('should be resolvable by injector if configured by user.', () { module((Module module) { diff --git a/test/directive/ng_model_spec.dart b/test/directive/ng_model_spec.dart index 8445713a2..2c9fef456 100644 --- a/test/directive/ng_model_spec.dart +++ b/test/directive/ng_model_spec.dart @@ -772,6 +772,32 @@ describe('ng-model', () { }); }); + + describe('reset()', () { + it('should reset the model value to its original state', () { + _.compile(''); + _.rootScope.$apply('myModel = "animal"'); + + Probe probe = _.rootScope.i; + var model = probe.directive(NgModel); + + expect(_.rootScope.myModel).toEqual('animal'); + expect(model.modelValue).toEqual('animal'); + expect(model.viewValue).toEqual('animal'); + + _.rootScope.$apply('myModel = "man"'); + + expect(_.rootScope.myModel).toEqual('man'); + expect(model.modelValue).toEqual('man'); + expect(model.viewValue).toEqual('man'); + + model.reset(); + + expect(_.rootScope.myModel).toEqual('animal'); + expect(model.modelValue).toEqual('animal'); + expect(model.viewValue).toEqual('animal'); + }); + }); }); @NgController(