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

Commit

Permalink
refactor(forms): remove registerWidget and use event instead
Browse files Browse the repository at this point in the history
Each widget (ng-model directive) emits $newFormControl event instead of getting hold of parent form
and calling form.registerWidget(this);
  • Loading branch information
vojtajina committed Mar 9, 2012
1 parent fae8446 commit e0c9551
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
28 changes: 7 additions & 21 deletions src/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function FormController($scope, name) {
$scope.$on('$destroy', function(event, widget) {
if (!widget) return;

if (widget.widgetId) {
if (widget.widgetId && form[widget.widgetId] === widget) {
delete form[widget.widgetId];
}
forEach(errors, removeWidget, widget);
Expand All @@ -60,6 +60,12 @@ function FormController($scope, name) {
form.pristine = false;
});

$scope.$on('$newFormControl', function(event, widget) {
if (widget.widgetId && !form.hasOwnProperty(widget.widgetId)) {
form[widget.widgetId] = widget;
}
});

// init state
form.dirty = false;
form.pristine = true;
Expand Down Expand Up @@ -95,26 +101,6 @@ function FormController($scope, name) {
}
}

/**
* @ngdoc function
* @name angular.module.ng.$compileProvider.directive.form.FormController#registerWidget
* @methodOf angular.module.ng.$compileProvider.directive.form.FormController
* @function
*
* @param {Object} widget Widget to register (controller of a widget)
* @param {string=} alias Name alias of the widget.
* (If specified, widget will be accesible as a form property)
*
* @description
*
*/
FormController.prototype.registerWidget = function(widget, alias) {
if (alias && !this.hasOwnProperty(alias)) {
widget.widgetId = alias;
this[alias] = widget;
}
};


/**
* @ngdoc directive
Expand Down
21 changes: 9 additions & 12 deletions src/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,8 @@ var inputDirective = [function() {
* @description
*
*/
var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
function($scope, $exceptionHandler, ngModel) {
var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel',
function($scope, $exceptionHandler, $attr, ngModel) {
this.viewValue = Number.NaN;
this.modelValue = Number.NaN;
this.parsers = [];
Expand All @@ -762,6 +762,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
this.valid = true;
this.invalid = false;
this.render = noop;
this.widgetId = $attr.name;


/**
Expand Down Expand Up @@ -920,26 +921,22 @@ var ngModelDirective = [function() {
inject: {
ngModel: 'accessor'
},
require: ['ngModel', '^?form'],
require: 'ngModel',
controller: NgModelController,
link: function(scope, element, attr, controllers) {
var modelController = controllers[0],
formController = controllers[1];

if (formController) {
formController.registerWidget(modelController, attr.name);
}
link: function(scope, element, attr, ctrl) {
// notify others, especially parent forms
scope.$emit('$newFormControl', ctrl);

forEach(['valid', 'invalid', 'pristine', 'dirty'], function(name) {
scope.$watch(function() {
return modelController[name];
return ctrl[name];
}, function(value) {
element[value ? 'addClass' : 'removeClass']('ng-' + name);
});
});

element.bind('$destroy', function() {
scope.$emit('$destroy', modelController);
scope.$emit('$destroy', ctrl);
});
}
};
Expand Down
6 changes: 5 additions & 1 deletion test/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ describe('NgModelController', function() {
var ctrl, scope, ngModelAccessor;

beforeEach(inject(function($rootScope, $controller) {
var attrs = {name: 'testAlias'};

scope = $rootScope;
ngModelAccessor = jasmine.createSpy('ngModel accessor');
ctrl = $controller(NgModelController, {$scope: scope, ngModel: ngModelAccessor});
ctrl = $controller(NgModelController, {$scope: scope, ngModel: ngModelAccessor, $attrs: attrs});

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

expect(ctrl.formatters).toEqual([]);
expect(ctrl.parsers).toEqual([]);

expect(ctrl.widgetId).toBe('testAlias');
});


Expand Down

0 comments on commit e0c9551

Please sign in to comment.