mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-15 18:23:09 +00:00
added better handling of ng:format=number
This commit is contained in:
parent
f09415d0de
commit
1087270c95
4 changed files with 51 additions and 19 deletions
|
|
@ -4,6 +4,8 @@ if (typeof document.getAttribute == 'undefined')
|
||||||
document.getAttribute = function() {};
|
document.getAttribute = function() {};
|
||||||
|
|
||||||
var consoleNode,
|
var consoleNode,
|
||||||
|
NULL = null,
|
||||||
|
UNDEFIEND = undefined,
|
||||||
PRIORITY_FIRST = -99999,
|
PRIORITY_FIRST = -99999,
|
||||||
PRIORITY_WATCH = -1000,
|
PRIORITY_WATCH = -1000,
|
||||||
PRIORITY_LAST = 99999,
|
PRIORITY_LAST = 99999,
|
||||||
|
|
@ -105,7 +107,7 @@ function jqLiteWrap(element) {
|
||||||
}
|
}
|
||||||
function isUndefined(value){ return typeof value == 'undefined'; }
|
function isUndefined(value){ return typeof value == 'undefined'; }
|
||||||
function isDefined(value){ return typeof value != 'undefined'; }
|
function isDefined(value){ return typeof value != 'undefined'; }
|
||||||
function isObject(value){ return typeof value == 'object';}
|
function isObject(value){ return value!=null && typeof value == 'object';}
|
||||||
function isString(value){ return typeof value == 'string';}
|
function isString(value){ return typeof value == 'string';}
|
||||||
function isNumber(value){ return typeof value == 'number';}
|
function isNumber(value){ return typeof value == 'number';}
|
||||||
function isArray(value) { return value instanceof Array; }
|
function isArray(value) { return value instanceof Array; }
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
function formatter(format, parse) {return {'format':format, 'parse':parse || format};}
|
function formatter(format, parse) {return {'format':format, 'parse':parse || format};}
|
||||||
function toString(obj) {return (isDefined(obj) && obj !== null) ? "" + obj : obj;}
|
function toString(obj) {
|
||||||
|
return (isDefined(obj) && obj !== null) ? "" + obj : obj;
|
||||||
|
}
|
||||||
|
|
||||||
var NUMBER = /^\s*[-+]?\d*(\.\d*)?\s*$/;
|
var NUMBER = /^\s*[-+]?\d*(\.\d*)?\s*$/;
|
||||||
|
|
||||||
|
|
@ -7,10 +9,11 @@ angularFormatter.noop = formatter(identity, identity);
|
||||||
angularFormatter.json = formatter(toJson, fromJson);
|
angularFormatter.json = formatter(toJson, fromJson);
|
||||||
angularFormatter['boolean'] = formatter(toString, toBoolean);
|
angularFormatter['boolean'] = formatter(toString, toBoolean);
|
||||||
angularFormatter.number = formatter(toString, function(obj){
|
angularFormatter.number = formatter(toString, function(obj){
|
||||||
if (isString(obj) && NUMBER.exec(obj)) {
|
if (obj == null || NUMBER.exec(obj)) {
|
||||||
return obj ? 1*obj : null;
|
return obj===null || obj === '' ? null : 1*obj;
|
||||||
|
} else {
|
||||||
|
throw "Not a number";
|
||||||
}
|
}
|
||||||
throw "Not a number";
|
|
||||||
});
|
});
|
||||||
|
|
||||||
angularFormatter.list = formatter(
|
angularFormatter.list = formatter(
|
||||||
|
|
|
||||||
|
|
@ -178,9 +178,18 @@ error = noop;
|
||||||
|
|
||||||
function click(element) {
|
function click(element) {
|
||||||
element = jqLite(element);
|
element = jqLite(element);
|
||||||
if ( msie &&
|
var type = lowercase(element.attr('type'));
|
||||||
nodeName(element) == 'INPUT' && (lowercase(element.attr('type')) == 'radio' || lowercase(element.attr('type')) == 'checkbox')) {
|
var name = lowercase(nodeName(element));
|
||||||
element[0].checked = ! element[0].checked;
|
if (msie) {
|
||||||
|
if (name == 'input') {
|
||||||
|
if (type == 'radio' || type == 'checkbox') {
|
||||||
|
element[0].checked = ! element[0].checked;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (name == 'option') {
|
||||||
|
JQLite.prototype.trigger.call(element.parent(), 'change');
|
||||||
|
} else {
|
||||||
|
JQLite.prototype.trigger.call(element, 'click');
|
||||||
}
|
}
|
||||||
JQLite.prototype.trigger.call(element, 'click');
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -338,17 +338,35 @@ describe("widget", function(){
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support type="select-one"', function(){
|
describe('select-one', function(){
|
||||||
compile(
|
it('should initialize to selected', function(){
|
||||||
'<select name="selection">' +
|
compile(
|
||||||
'<option>A</option>' +
|
'<select name="selection">' +
|
||||||
'<option selected>B</option>' +
|
'<option>A</option>' +
|
||||||
|
'<option selected>B</option>' +
|
||||||
'</select>');
|
'</select>');
|
||||||
expect(scope.selection).toEqual('B');
|
expect(scope.selection).toEqual('B');
|
||||||
scope.selection = 'A';
|
scope.selection = 'A';
|
||||||
scope.$eval();
|
scope.$eval();
|
||||||
expect(scope.selection).toEqual('A');
|
expect(scope.selection).toEqual('A');
|
||||||
expect(element[0].childNodes[0].selected).toEqual(true);
|
expect(element[0].childNodes[0].selected).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should honor the value field in option', function(){
|
||||||
|
compile(
|
||||||
|
'<select name="selection" ng:format="number">' +
|
||||||
|
'<option value="{{$index}}" ng:repeat="name in [\'A\', \'B\']">{{name}}</option>' +
|
||||||
|
'</select>');
|
||||||
|
// childNodes[0] is repeater
|
||||||
|
expect(scope.selection).toEqual(undefined);
|
||||||
|
|
||||||
|
click(element[0].childNodes[1]);
|
||||||
|
expect(scope.selection).toEqual(0);
|
||||||
|
|
||||||
|
scope.selection = 1;
|
||||||
|
scope.$eval();
|
||||||
|
expect(element[0].childNodes[2].selected).toEqual(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support type="select-multiple"', function(){
|
it('should support type="select-multiple"', function(){
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue