diff options
| author | Vojta Jina | 2012-03-08 15:03:24 -0800 | 
|---|---|---|
| committer | Vojta Jina | 2012-03-09 10:10:28 -0800 | 
| commit | e0c9551fd7c716d9d71ed6f4f1d2f5911f53032c (patch) | |
| tree | f98632d66538460b1405d217342f483de6d519df /src | |
| parent | fae84463e45f18eabdabc4c1c7378e23ad35842c (diff) | |
| download | angular.js-e0c9551fd7c716d9d71ed6f4f1d2f5911f53032c.tar.bz2 | |
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);
Diffstat (limited to 'src')
| -rw-r--r-- | src/directive/form.js | 28 | ||||
| -rw-r--r-- | src/directive/input.js | 21 | 
2 files changed, 16 insertions, 33 deletions
| diff --git a/src/directive/form.js b/src/directive/form.js index c3e6b21d..e3b937da 100644 --- a/src/directive/form.js +++ b/src/directive/form.js @@ -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); @@ -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; @@ -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 diff --git a/src/directive/input.js b/src/directive/input.js index ae3fea1c..8eea48a3 100644 --- a/src/directive/input.js +++ b/src/directive/input.js @@ -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 = []; @@ -762,6 +762,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',    this.valid = true;    this.invalid = false;    this.render = noop; +  this.widgetId = $attr.name;    /** @@ -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);        });      }    }; | 
