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

Commit 1087270

Browse files
committed
added better handling of ng:format=number
1 parent f09415d commit 1087270

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

src/Angular.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ if (typeof document.getAttribute == 'undefined')
44
document.getAttribute = function() {};
55

66
var consoleNode,
7+
NULL = null,
8+
UNDEFIEND = undefined,
79
PRIORITY_FIRST = -99999,
810
PRIORITY_WATCH = -1000,
911
PRIORITY_LAST = 99999,
@@ -105,7 +107,7 @@ function jqLiteWrap(element) {
105107
}
106108
function isUndefined(value){ return typeof value == 'undefined'; }
107109
function isDefined(value){ return typeof value != 'undefined'; }
108-
function isObject(value){ return typeof value == 'object';}
110+
function isObject(value){ return value!=null && typeof value == 'object';}
109111
function isString(value){ return typeof value == 'string';}
110112
function isNumber(value){ return typeof value == 'number';}
111113
function isArray(value) { return value instanceof Array; }

src/formatters.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
function formatter(format, parse) {return {'format':format, 'parse':parse || format};}
2-
function toString(obj) {return (isDefined(obj) && obj !== null) ? "" + obj : obj;}
2+
function toString(obj) {
3+
return (isDefined(obj) && obj !== null) ? "" + obj : obj;
4+
}
35

46
var NUMBER = /^\s*[-+]?\d*(\.\d*)?\s*$/;
57

68
angularFormatter.noop = formatter(identity, identity);
79
angularFormatter.json = formatter(toJson, fromJson);
810
angularFormatter['boolean'] = formatter(toString, toBoolean);
911
angularFormatter.number = formatter(toString, function(obj){
10-
if (isString(obj) && NUMBER.exec(obj)) {
11-
return obj ? 1*obj : null;
12+
if (obj == null || NUMBER.exec(obj)) {
13+
return obj===null || obj === '' ? null : 1*obj;
14+
} else {
15+
throw "Not a number";
1216
}
13-
throw "Not a number";
1417
});
1518

1619
angularFormatter.list = formatter(

test/testabilityPatch.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,18 @@ error = noop;
178178

179179
function click(element) {
180180
element = jqLite(element);
181-
if ( msie &&
182-
nodeName(element) == 'INPUT' && (lowercase(element.attr('type')) == 'radio' || lowercase(element.attr('type')) == 'checkbox')) {
183-
element[0].checked = ! element[0].checked;
181+
var type = lowercase(element.attr('type'));
182+
var name = lowercase(nodeName(element));
183+
if (msie) {
184+
if (name == 'input') {
185+
if (type == 'radio' || type == 'checkbox') {
186+
element[0].checked = ! element[0].checked;
187+
}
188+
}
189+
}
190+
if (name == 'option') {
191+
JQLite.prototype.trigger.call(element.parent(), 'change');
192+
} else {
193+
JQLite.prototype.trigger.call(element, 'click');
184194
}
185-
JQLite.prototype.trigger.call(element, 'click');
186195
}

test/widgetsSpec.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,35 @@ describe("widget", function(){
338338

339339
});
340340

341-
it('should support type="select-one"', function(){
342-
compile(
343-
'<select name="selection">' +
344-
'<option>A</option>' +
345-
'<option selected>B</option>' +
341+
describe('select-one', function(){
342+
it('should initialize to selected', function(){
343+
compile(
344+
'<select name="selection">' +
345+
'<option>A</option>' +
346+
'<option selected>B</option>' +
346347
'</select>');
347-
expect(scope.selection).toEqual('B');
348-
scope.selection = 'A';
349-
scope.$eval();
350-
expect(scope.selection).toEqual('A');
351-
expect(element[0].childNodes[0].selected).toEqual(true);
348+
expect(scope.selection).toEqual('B');
349+
scope.selection = 'A';
350+
scope.$eval();
351+
expect(scope.selection).toEqual('A');
352+
expect(element[0].childNodes[0].selected).toEqual(true);
353+
});
354+
355+
it('should honor the value field in option', function(){
356+
compile(
357+
'<select name="selection" ng:format="number">' +
358+
'<option value="{{$index}}" ng:repeat="name in [\'A\', \'B\']">{{name}}</option>' +
359+
'</select>');
360+
// childNodes[0] is repeater
361+
expect(scope.selection).toEqual(undefined);
362+
363+
click(element[0].childNodes[1]);
364+
expect(scope.selection).toEqual(0);
365+
366+
scope.selection = 1;
367+
scope.$eval();
368+
expect(element[0].childNodes[2].selected).toEqual(true);
369+
});
352370
});
353371

354372
it('should support type="select-multiple"', function(){

0 commit comments

Comments
 (0)