Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(forms): provide support for reseting forms, fieldsets and models
Browse files Browse the repository at this point in the history
  • Loading branch information
matsko committed Feb 13, 2014
1 parent eaaa337 commit c75202d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/directive/ng_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ abstract class NgControl implements NgDetachAware {
}
}

reset() {
_scope.$broadcast('resetNgModel');
}

get name => _name;
set name(value) {
_name = value;
Expand Down Expand Up @@ -168,5 +172,6 @@ class NgNullControl implements NgControl {
get invalid => null;
set invalid(value) {}

reset() => null;
detach() => null;
}
10 changes: 10 additions & 0 deletions lib/directive/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NgModel extends NgControl {
BoundGetter getter = ([_]) => null;
BoundSetter setter = (_, [__]) => null;

var _lastValue;
String _exp;
final _validators = <NgValidatable>[];

Expand All @@ -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')
Expand All @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions test/directive/ng_form_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,37 @@ describe('form', () {
}));
});

describe('reset()', () {
it('should reset the model value to its original state', inject((TestBed _) {
_.compile('<form name="superForm">' +
' <input type="text" ng-model="myModel" probe="i" />' +
'</form>');
_.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) {
Expand Down
26 changes: 26 additions & 0 deletions test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,32 @@ describe('ng-model', () {

});
});

describe('reset()', () {
it('should reset the model value to its original state', () {
_.compile('<input type="text" ng-model="myModel" probe="i" />');
_.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(
Expand Down

0 comments on commit c75202d

Please sign in to comment.