Closes #153: input widgets without name are ignored

This commit is contained in:
Misko Hevery 2010-12-09 22:09:46 -08:00
parent b225083a21
commit ec4d446f89
3 changed files with 51 additions and 37 deletions

View file

@ -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) #

View file

@ -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);
}
});
};
}

View file

@ -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(){