diff options
| author | Misko Hevery | 2010-05-13 16:40:41 -0700 | 
|---|---|---|
| committer | Misko Hevery | 2010-05-13 16:40:41 -0700 | 
| commit | 1bdcf72e456c74256b14f98b26e969b9de637614 (patch) | |
| tree | 5e99231d79b96f25fc1afd69e4ac4c4fe3ef3cd7 | |
| parent | 22d1464d7abc284dd56d6beaf47e8d85088e01c5 (diff) | |
| download | angular.js-1bdcf72e456c74256b14f98b26e969b9de637614.tar.bz2 | |
put formatters back.
| -rw-r--r-- | src/widgets.js | 23 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 64 | 
2 files changed, 65 insertions, 22 deletions
| diff --git a/src/widgets.js b/src/widgets.js index 7de4bdc1..1c9fe605 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -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???      }; diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index ecea6223..17120682 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -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(){ | 
