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

Commit bbbba4b

Browse files
matskomhevery
authored andcommitted
refactor(forms): remove scope event communication between controls
1 parent 3f639b7 commit bbbba4b

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

lib/directive/ng_control.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
1616
bool _valid;
1717
bool _submit_valid;
1818

19-
final Scope _scope;
2019
final NgControl _parentControl;
2120
final NgAnimate _animate;
2221
dom.Element _element;
@@ -25,14 +24,12 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
2524
final List<NgControl> _controls = new List<NgControl>();
2625
final Map<String, List<NgControl>> _controlByName = new Map<String, List<NgControl>>();
2726

28-
NgControl(Scope this._scope, dom.Element this._element, Injector injector,
27+
NgControl(dom.Element this._element, Injector injector,
2928
NgAnimate this._animate)
3029
: _parentControl = injector.parent.get(NgControl)
3130
{
3231
pristine = true;
3332
untouched = true;
34-
35-
_scope.on('submitNgControl').listen((e) => _onSubmit(e.data));
3633
}
3734

3835
@override
@@ -49,13 +46,15 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
4946
}
5047

5148
reset() {
52-
_scope.broadcast('resetNgModel');
5349
untouched = true;
50+
_controls.forEach((control) {
51+
control.reset();
52+
});
5453
}
5554

5655
bool hasError(String key) => errors.containsKey(key);
5756

58-
_onSubmit(bool valid) {
57+
onSubmit(bool valid) {
5958
if (valid) {
6059
_submit_valid = true;
6160
_animate.addClass(element, NG_SUBMIT_VALID_CLASS);
@@ -65,6 +64,9 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
6564
_animate.addClass(element, NG_SUBMIT_INVALID_CLASS);
6665
_animate.removeClass(element, NG_SUBMIT_VALID_CLASS);
6766
}
67+
_controls.forEach((control) {
68+
control.onSubmit(valid);
69+
});
6870
}
6971

7072
get submitted => _submit_valid != null;
@@ -198,14 +200,13 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
198200
}
199201

200202
class NgNullControl implements NgControl {
201-
var _name, _dirty, _valid, _invalid, _submit_valid, _pristine, _element;
202-
var _touched, _untouched;
203-
var _controls, _scope, _parentControl, _controlName, _animate;
203+
var _name, _dirty, _valid, _submit_valid, _pristine, _element, _touched;
204+
var _controls, _parentControl, _controlName, _animate;
204205
var errors, _controlByName;
205206
dom.Element element;
206207

207208
NgNullControl() {}
208-
_onSubmit(bool valid) {}
209+
onSubmit(bool valid) {}
209210

210211
addControl(control) {}
211212
removeControl(control) {}

lib/directive/ng_form.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ part of angular.directive;
2323
map: const { 'ng-form': '@name' },
2424
visibility: NgDirective.CHILDREN_VISIBILITY)
2525
class NgForm extends NgControl {
26+
final Scope _scope;
27+
2628
/**
2729
* Instantiates a new instance of NgForm. Upon creation, the instance of the
2830
* class will be bound to the formName property on the scope (where formName
@@ -33,14 +35,14 @@ class NgForm extends NgControl {
3335
* * [element] - The form DOM element.
3436
* * [injector] - An instance of Injector.
3537
*/
36-
NgForm(Scope scope, dom.Element element, Injector injector,
38+
NgForm(this._scope, dom.Element element, Injector injector,
3739
NgAnimate animate) :
38-
super(scope, element, injector, animate) {
40+
super(element, injector, animate) {
3941

4042
if (!element.attributes.containsKey('action')) {
4143
element.onSubmit.listen((event) {
4244
event.preventDefault();
43-
_scope.broadcast('submitNgControl', valid == true);
45+
onSubmit(valid == true);
4446
if (valid == true) {
4547
reset();
4648
}
@@ -76,6 +78,8 @@ class NgForm extends NgControl {
7678
}
7779

7880
class NgNullForm extends NgNullControl implements NgForm {
81+
var _scope;
82+
7983
NgNullForm() {}
8084
operator []=(String key, value) {}
8185
operator[](name) {}

lib/directive/ng_model.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ part of angular.directive;
1414
class NgModel extends NgControl implements NgAttachAware {
1515
final NgForm _form;
1616
final AstParser _parser;
17+
final Scope _scope;
1718

1819
BoundGetter getter = ([_]) => null;
1920
BoundSetter setter = (_, [__]) => null;
@@ -26,9 +27,9 @@ class NgModel extends NgControl implements NgAttachAware {
2627
bool _watchCollection;
2728
Function render = (value) => null;
2829

29-
NgModel(Scope _scope, dom.Element _element, Injector injector,
30+
NgModel(this._scope, dom.Element _element, Injector injector,
3031
NgForm this._form, this._parser, NodeAttrs attrs, NgAnimate animate)
31-
: super(_scope, _element, injector, animate)
32+
: super(_element, injector, animate)
3233
{
3334
_exp = attrs["ng-model"];
3435
watchCollection = false;
@@ -41,16 +42,15 @@ class NgModel extends NgControl implements NgAttachAware {
4142

4243
attach() {
4344
watchCollection = false;
44-
_scope.on('resetNgModel').listen((e) => reset());
4545
}
4646

4747
reset() {
4848
untouched = true;
4949
modelValue = _lastValue;
5050
}
5151

52-
_onSubmit(bool valid) {
53-
super._onSubmit(valid);
52+
onSubmit(bool valid) {
53+
super.onSubmit(valid);
5454
if (valid) {
5555
_lastValue = modelValue;
5656
}

0 commit comments

Comments
 (0)