mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
put formatters back.
This commit is contained in:
parent
22d1464d7a
commit
1bdcf72e45
2 changed files with 65 additions and 22 deletions
|
|
@ -13,6 +13,21 @@ function modelAccessor(scope, element) {
|
|||
};
|
||||
}
|
||||
|
||||
function modelFormattedAccessor(scope, element) {
|
||||
var accessor = modelAccessor(scope, element),
|
||||
farmatterName = element.attr('ng-format') || NOOP,
|
||||
formatter = angularFormatter(farmatterName);
|
||||
if (!formatter) throw "Formatter named '" + farmatterName + "' not found.";
|
||||
return {
|
||||
get: function() {
|
||||
return formatter.format(accessor.get());
|
||||
},
|
||||
set: function(value) {
|
||||
return accessor.set(formatter.parse(value));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function compileValidator(expr) {
|
||||
return new Parser(expr).validator()();
|
||||
}
|
||||
|
|
@ -134,10 +149,10 @@ var textWidget = inputWidget('keyup change', modelAccessor, valueAccessor, initW
|
|||
'submit': buttonWidget,
|
||||
'reset': buttonWidget,
|
||||
'image': buttonWidget,
|
||||
'checkbox': inputWidget('click', modelAccessor, checkedAccessor, initWidgetValue(false)),
|
||||
'radio': inputWidget('click', modelAccessor, radioAccessor, radioInit),
|
||||
'select-one': inputWidget('change', modelAccessor, valueAccessor, initWidgetValue(null)),
|
||||
'select-multiple': inputWidget('change', modelAccessor, optionsAccessor, initWidgetValue([]))
|
||||
'checkbox': inputWidget('click', modelFormattedAccessor, checkedAccessor, initWidgetValue(false)),
|
||||
'radio': inputWidget('click', modelFormattedAccessor, radioAccessor, radioInit),
|
||||
'select-one': inputWidget('change', modelFormattedAccessor, valueAccessor, initWidgetValue(null)),
|
||||
'select-multiple': inputWidget('change', modelFormattedAccessor, optionsAccessor, initWidgetValue([]))
|
||||
// 'file': fileWidget???
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -56,14 +56,6 @@ describe("widget", function(){
|
|||
expect(scope.$get('list')).toEqual(['1', '2', '3']);
|
||||
});
|
||||
|
||||
it("should format booleans", function(){
|
||||
compile('<input type="checkbox" name="name" value="true" ng-format="boolean"/>', function(){
|
||||
scope.name = false;
|
||||
});
|
||||
expect(scope.name).toEqual(false);
|
||||
expect(scope.$element[0].checked).toEqual(false);
|
||||
});
|
||||
|
||||
it("should come up blank if null", function(){
|
||||
compile('<input type="text" name="age" ng-format="number"/>', function(){
|
||||
scope.age = null;
|
||||
|
|
@ -123,6 +115,52 @@ describe("widget", function(){
|
|||
|
||||
});
|
||||
|
||||
describe("checkbox", function(){
|
||||
it("should format booleans", function(){
|
||||
compile('<input type="checkbox" name="name"/>', function(){
|
||||
scope.name = false;
|
||||
});
|
||||
expect(scope.name).toEqual(false);
|
||||
expect(scope.$element[0].checked).toEqual(false);
|
||||
});
|
||||
|
||||
it('should support type="checkbox"', function(){
|
||||
compile('<input type="checkBox" name="checkbox" checked ng-change="action = true"/>');
|
||||
expect(scope.checkbox).toEqual(true);
|
||||
click(element);
|
||||
expect(scope.checkbox).toEqual(false);
|
||||
expect(scope.action).toEqual(true);
|
||||
click(element);
|
||||
expect(scope.checkbox).toEqual(true);
|
||||
});
|
||||
|
||||
it("should use ng-format", function(){
|
||||
angularFormatter('testFormat', {
|
||||
parse: function(value){
|
||||
return value ? "Worked" : "Failed";
|
||||
},
|
||||
|
||||
format: function(value) {
|
||||
if (value == undefined) return value;
|
||||
return value == "Worked";
|
||||
}
|
||||
|
||||
});
|
||||
compile('<input type="checkbox" name="state" ng-format="testFormat" checked/>');
|
||||
expect(scope.state).toEqual("Worked");
|
||||
expect(scope.$element[0].checked).toEqual(true);
|
||||
|
||||
click(scope.$element);
|
||||
expect(scope.state).toEqual("Failed");
|
||||
expect(scope.$element[0].checked).toEqual(false);
|
||||
|
||||
scope.state = "Worked";
|
||||
scope.$eval();
|
||||
expect(scope.state).toEqual("Worked");
|
||||
expect(scope.$element[0].checked).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ng-validate", function(){
|
||||
it("should process ng-validate", function(){
|
||||
compile('<input type="text" name="price" value="abc" ng-validate="number"/>');
|
||||
|
|
@ -212,16 +250,6 @@ describe("widget", function(){
|
|||
expect(scope.$get('clicked')).toEqual(true);
|
||||
});
|
||||
|
||||
it('should support type="checkbox"', function(){
|
||||
compile('<input type="checkBox" name="checkbox" checked ng-change="action = true"/>');
|
||||
expect(scope.checkbox).toEqual(true);
|
||||
click(element);
|
||||
expect(scope.checkbox).toEqual(false);
|
||||
expect(scope.action).toEqual(true);
|
||||
click(element);
|
||||
expect(scope.checkbox).toEqual(true);
|
||||
});
|
||||
|
||||
describe('radio', function(){
|
||||
|
||||
it('should support type="radio"', function(){
|
||||
|
|
|
|||
Loading…
Reference in a new issue