diff options
| author | Misko Hevery | 2010-12-09 22:09:46 -0800 |
|---|---|---|
| committer | Misko Hevery | 2010-12-11 08:28:11 -0800 |
| commit | ec4d446f898e7860c12a337200c31c3b75f663cc (patch) | |
| tree | b8cca66bbeae20285b38314dd518ca04e64762d6 | |
| parent | b225083a21844bdc710b02337dfa1bef5883baf3 (diff) | |
| download | angular.js-ec4d446f898e7860c12a337200c31c3b75f663cc.tar.bz2 | |
Closes #153: input widgets without name are ignored
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | src/widgets.js | 73 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 13 |
3 files changed, 51 insertions, 37 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9456bcba..f4c911fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # <angular/> 0.9.8 astral-projection (in-progress) # +### Bug Fixes +- Ignore input widgets which have no name (issue #153) # <angular/> 0.9.7 sonic-scream (2010-12-10) # diff --git a/src/widgets.js b/src/widgets.js index 2d9f53f7..532627eb 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -134,17 +134,18 @@ function modelAccessor(scope, element) { var expr = element.attr('name'); - if (!expr) throw "Required field 'name' not found."; - return { - get: function() { - return scope.$eval(expr); - }, - set: function(value) { - if (value !== _undefined) { - return scope.$tryEval(expr + '=' + toJson(value), element); + if (expr) { + return { + get: function() { + return scope.$eval(expr); + }, + set: function(value) { + if (value !== _undefined) { + return scope.$tryEval(expr + '=' + toJson(value), element); + } } - } - }; + }; + } } function modelFormattedAccessor(scope, element) { @@ -152,14 +153,16 @@ function modelFormattedAccessor(scope, element) { formatterName = element.attr('ng:format') || NOOP, formatter = angularFormatter(formatterName); if (!formatter) throw "Formatter named '" + formatterName + "' not found."; - return { - get: function() { - return formatter.format(accessor.get()); - }, - set: function(value) { - return accessor.set(formatter.parse(value)); - } - }; + if (accessor) { + return { + get: function() { + return formatter.format(accessor.get()); + }, + set: function(value) { + return accessor.set(formatter.parse(value)); + } + }; + } } function compileValidator(expr) { @@ -458,25 +461,27 @@ function inputWidget(events, modelAccessor, viewAccessor, initFn, dirtyChecking) view = viewAccessor(scope, element), action = element.attr('ng:change') || '', lastValue; - initFn.call(scope, model, view, element); - this.$eval(element.attr('ng:init')||''); - // Don't register a handler if we are a button (noopAccessor) and there is no action - if (action || modelAccessor !== noopAccessor) { - element.bind(events, function (){ - var value = view.get(); - if (!dirtyChecking || value != lastValue) { - model.set(value); - lastValue = model.get(); - scope.$tryEval(action, element); - scope.$root.$eval(); + if (model) { + initFn.call(scope, model, view, element); + this.$eval(element.attr('ng:init')||''); + // Don't register a handler if we are a button (noopAccessor) and there is no action + if (action || modelAccessor !== noopAccessor) { + element.bind(events, function (){ + var value = view.get(); + if (!dirtyChecking || value != lastValue) { + model.set(value); + lastValue = model.get(); + scope.$tryEval(action, element); + scope.$root.$eval(); + } + }); + } + scope.$watch(model.get, function(value){ + if (lastValue !== value) { + view.set(lastValue = value); } }); } - scope.$watch(model.get, function(value){ - if (lastValue !== value) { - view.set(lastValue = value); - } - }); }; } diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index d6cee491..cfda67d1 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -452,10 +452,17 @@ describe("widget", function(){ scope.$eval(); expect(element[0].childNodes[0].selected).toEqual(true); }); - - it('should report error on missing field', function(){ + + it('should ignore text widget which have no name', function(){ compile('<input type="text"/>'); - expect(element.hasClass('ng-exception')).toBeTruthy(); + expect(scope.$element.attr('ng-exception')).toBeFalsy(); + expect(scope.$element.hasClass('ng-exception')).toBeFalsy(); + }); + + it('should ignore checkbox widget which have no name', function(){ + compile('<input type="checkbox"/>'); + expect(scope.$element.attr('ng-exception')).toBeFalsy(); + expect(scope.$element.hasClass('ng-exception')).toBeFalsy(); }); it('should report error on assignment error', function(){ |
