Skip to content

Commit ec4d446

Browse files
committed
Closes angular#153: input widgets without name are ignored
1 parent b225083 commit ec4d446

File tree

3 files changed

+51
-37
lines changed

3 files changed

+51
-37
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# <angular/> 0.9.8 astral-projection (in-progress) #
22

3+
### Bug Fixes
4+
- Ignore input widgets which have no name (issue #153)
35

46
# <angular/> 0.9.7 sonic-scream (2010-12-10) #
57

src/widgets.js

+39-34
Original file line numberDiff line numberDiff line change
@@ -134,32 +134,35 @@
134134

135135
function modelAccessor(scope, element) {
136136
var expr = element.attr('name');
137-
if (!expr) throw "Required field 'name' not found.";
138-
return {
139-
get: function() {
140-
return scope.$eval(expr);
141-
},
142-
set: function(value) {
143-
if (value !== _undefined) {
144-
return scope.$tryEval(expr + '=' + toJson(value), element);
137+
if (expr) {
138+
return {
139+
get: function() {
140+
return scope.$eval(expr);
141+
},
142+
set: function(value) {
143+
if (value !== _undefined) {
144+
return scope.$tryEval(expr + '=' + toJson(value), element);
145+
}
145146
}
146-
}
147-
};
147+
};
148+
}
148149
}
149150

150151
function modelFormattedAccessor(scope, element) {
151152
var accessor = modelAccessor(scope, element),
152153
formatterName = element.attr('ng:format') || NOOP,
153154
formatter = angularFormatter(formatterName);
154155
if (!formatter) throw "Formatter named '" + formatterName + "' not found.";
155-
return {
156-
get: function() {
157-
return formatter.format(accessor.get());
158-
},
159-
set: function(value) {
160-
return accessor.set(formatter.parse(value));
161-
}
162-
};
156+
if (accessor) {
157+
return {
158+
get: function() {
159+
return formatter.format(accessor.get());
160+
},
161+
set: function(value) {
162+
return accessor.set(formatter.parse(value));
163+
}
164+
};
165+
}
163166
}
164167

165168
function compileValidator(expr) {
@@ -458,25 +461,27 @@ function inputWidget(events, modelAccessor, viewAccessor, initFn, dirtyChecking)
458461
view = viewAccessor(scope, element),
459462
action = element.attr('ng:change') || '',
460463
lastValue;
461-
initFn.call(scope, model, view, element);
462-
this.$eval(element.attr('ng:init')||'');
463-
// Don't register a handler if we are a button (noopAccessor) and there is no action
464-
if (action || modelAccessor !== noopAccessor) {
465-
element.bind(events, function (){
466-
var value = view.get();
467-
if (!dirtyChecking || value != lastValue) {
468-
model.set(value);
469-
lastValue = model.get();
470-
scope.$tryEval(action, element);
471-
scope.$root.$eval();
464+
if (model) {
465+
initFn.call(scope, model, view, element);
466+
this.$eval(element.attr('ng:init')||'');
467+
// Don't register a handler if we are a button (noopAccessor) and there is no action
468+
if (action || modelAccessor !== noopAccessor) {
469+
element.bind(events, function (){
470+
var value = view.get();
471+
if (!dirtyChecking || value != lastValue) {
472+
model.set(value);
473+
lastValue = model.get();
474+
scope.$tryEval(action, element);
475+
scope.$root.$eval();
476+
}
477+
});
478+
}
479+
scope.$watch(model.get, function(value){
480+
if (lastValue !== value) {
481+
view.set(lastValue = value);
472482
}
473483
});
474484
}
475-
scope.$watch(model.get, function(value){
476-
if (lastValue !== value) {
477-
view.set(lastValue = value);
478-
}
479-
});
480485
};
481486
}
482487

test/widgetsSpec.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,17 @@ describe("widget", function(){
452452
scope.$eval();
453453
expect(element[0].childNodes[0].selected).toEqual(true);
454454
});
455-
456-
it('should report error on missing field', function(){
455+
456+
it('should ignore text widget which have no name', function(){
457457
compile('<input type="text"/>');
458-
expect(element.hasClass('ng-exception')).toBeTruthy();
458+
expect(scope.$element.attr('ng-exception')).toBeFalsy();
459+
expect(scope.$element.hasClass('ng-exception')).toBeFalsy();
460+
});
461+
462+
it('should ignore checkbox widget which have no name', function(){
463+
compile('<input type="checkbox"/>');
464+
expect(scope.$element.attr('ng-exception')).toBeFalsy();
465+
expect(scope.$element.hasClass('ng-exception')).toBeFalsy();
459466
});
460467

461468
it('should report error on assignment error', function(){

0 commit comments

Comments
 (0)