Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e0c9551

Browse files
committed
refactor(forms): remove registerWidget and use event instead
Each widget (ng-model directive) emits $newFormControl event instead of getting hold of parent form and calling form.registerWidget(this);
1 parent fae8446 commit e0c9551

File tree

3 files changed

+21
-34
lines changed

3 files changed

+21
-34
lines changed

src/directive/form.js

+7-21
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function FormController($scope, name) {
3333
$scope.$on('$destroy', function(event, widget) {
3434
if (!widget) return;
3535

36-
if (widget.widgetId) {
36+
if (widget.widgetId && form[widget.widgetId] === widget) {
3737
delete form[widget.widgetId];
3838
}
3939
forEach(errors, removeWidget, widget);
@@ -60,6 +60,12 @@ function FormController($scope, name) {
6060
form.pristine = false;
6161
});
6262

63+
$scope.$on('$newFormControl', function(event, widget) {
64+
if (widget.widgetId && !form.hasOwnProperty(widget.widgetId)) {
65+
form[widget.widgetId] = widget;
66+
}
67+
});
68+
6369
// init state
6470
form.dirty = false;
6571
form.pristine = true;
@@ -95,26 +101,6 @@ function FormController($scope, name) {
95101
}
96102
}
97103

98-
/**
99-
* @ngdoc function
100-
* @name angular.module.ng.$compileProvider.directive.form.FormController#registerWidget
101-
* @methodOf angular.module.ng.$compileProvider.directive.form.FormController
102-
* @function
103-
*
104-
* @param {Object} widget Widget to register (controller of a widget)
105-
* @param {string=} alias Name alias of the widget.
106-
* (If specified, widget will be accesible as a form property)
107-
*
108-
* @description
109-
*
110-
*/
111-
FormController.prototype.registerWidget = function(widget, alias) {
112-
if (alias && !this.hasOwnProperty(alias)) {
113-
widget.widgetId = alias;
114-
this[alias] = widget;
115-
}
116-
};
117-
118104

119105
/**
120106
* @ngdoc directive

src/directive/input.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,8 @@ var inputDirective = [function() {
750750
* @description
751751
*
752752
*/
753-
var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
754-
function($scope, $exceptionHandler, ngModel) {
753+
var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel',
754+
function($scope, $exceptionHandler, $attr, ngModel) {
755755
this.viewValue = Number.NaN;
756756
this.modelValue = Number.NaN;
757757
this.parsers = [];
@@ -762,6 +762,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
762762
this.valid = true;
763763
this.invalid = false;
764764
this.render = noop;
765+
this.widgetId = $attr.name;
765766

766767

767768
/**
@@ -920,26 +921,22 @@ var ngModelDirective = [function() {
920921
inject: {
921922
ngModel: 'accessor'
922923
},
923-
require: ['ngModel', '^?form'],
924+
require: 'ngModel',
924925
controller: NgModelController,
925-
link: function(scope, element, attr, controllers) {
926-
var modelController = controllers[0],
927-
formController = controllers[1];
928-
929-
if (formController) {
930-
formController.registerWidget(modelController, attr.name);
931-
}
926+
link: function(scope, element, attr, ctrl) {
927+
// notify others, especially parent forms
928+
scope.$emit('$newFormControl', ctrl);
932929

933930
forEach(['valid', 'invalid', 'pristine', 'dirty'], function(name) {
934931
scope.$watch(function() {
935-
return modelController[name];
932+
return ctrl[name];
936933
}, function(value) {
937934
element[value ? 'addClass' : 'removeClass']('ng-' + name);
938935
});
939936
});
940937

941938
element.bind('$destroy', function() {
942-
scope.$emit('$destroy', modelController);
939+
scope.$emit('$destroy', ctrl);
943940
});
944941
}
945942
};

test/directive/inputSpec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ describe('NgModelController', function() {
44
var ctrl, scope, ngModelAccessor;
55

66
beforeEach(inject(function($rootScope, $controller) {
7+
var attrs = {name: 'testAlias'};
8+
79
scope = $rootScope;
810
ngModelAccessor = jasmine.createSpy('ngModel accessor');
9-
ctrl = $controller(NgModelController, {$scope: scope, ngModel: ngModelAccessor});
11+
ctrl = $controller(NgModelController, {$scope: scope, ngModel: ngModelAccessor, $attrs: attrs});
1012

1113
// mock accessor (locals)
1214
ngModelAccessor.andCallFake(function(val) {
@@ -27,6 +29,8 @@ describe('NgModelController', function() {
2729

2830
expect(ctrl.formatters).toEqual([]);
2931
expect(ctrl.parsers).toEqual([]);
32+
33+
expect(ctrl.widgetId).toBe('testAlias');
3034
});
3135

3236

0 commit comments

Comments
 (0)