From 3fe1fc02e67abdc241bcc4a192b547bed3d5afcb Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Tue, 14 Aug 2012 19:31:22 +0200 Subject: [PATCH] Fixes for the reset issue #149 --- modules/directives/reset/reset.js | 25 ++++---- modules/directives/reset/test/resetSpec.js | 71 +++++++++++----------- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/modules/directives/reset/reset.js b/modules/directives/reset/reset.js index 00465af..2143a0c 100644 --- a/modules/directives/reset/reset.js +++ b/modules/directives/reset/reset.js @@ -1,18 +1,21 @@ - /** * Add a clear button to form inputs to reset their value */ -angular.module('ui.directives').directive('uiReset', ['$parse', function($parse) { +angular.module('ui.directives').directive('uiReset', ['$parse', function ($parse) { return { require:'ngModel', - link: function(scope, elm, attrs, ctrl) { - elm.wrap('').after('').next().click(function(e){ - e.preventDefault(); - scope.$apply(function(){ - // This lets you SET the value of the 'parsed' model - ctrl.$setViewValue(null); - }); - }); - } + link:function (scope, elm, attrs, ctrl) { + var aElement = angular.element(''); + elm.wrap('').after(aElement); + + aElement.bind('click', function (e) { + e.preventDefault(); + scope.$apply(function () { + // This lets you SET the value of the 'parsed' model + ctrl.$setViewValue(null); + ctrl.$render(); + }); + }); + } }; }]); diff --git a/modules/directives/reset/test/resetSpec.js b/modules/directives/reset/test/resetSpec.js index 1557b92..8169853 100644 --- a/modules/directives/reset/test/resetSpec.js +++ b/modules/directives/reset/test/resetSpec.js @@ -1,41 +1,44 @@ /*global describe, beforeEach, module, inject, it, spyOn, expect, $ */ describe('uiReset', function () { - 'use strict'; + 'use strict'; - var scope, $compile; - beforeEach(module('ui.directives')); - beforeEach(inject(function (_$rootScope_, _$compile_, _$window_) { - scope = _$rootScope_.$new(); - $compile = _$compile_; - })); + var scope, $compile; + beforeEach(module('ui.directives')); + beforeEach(inject(function (_$rootScope_, _$compile_, _$window_) { + scope = _$rootScope_.$new(); + $compile = _$compile_; + })); - describe('compiling this directive', function() { - it('should throw an error if we have no model defined', function() { - function compile() { - $compile('')(scope); - } - expect(compile).toThrow(); - }); - it('should insert some crap into the DOM', function() { - scope.foo = 'bar'; - scope.$apply(); - var element = $compile('')(scope); - expect(element.parent().is('span')).toBe(true); - }); + describe('compiling this directive', function () { + it('should throw an error if we have no model defined', function () { + function compile() { + $compile('')(scope); + } + expect(compile).toThrow(); }); - describe('clicking on the created anchor tag', function() { - it('should prevent the default action', function() { - var element = $compile('')(scope); - spyOn($.Event.prototype, 'preventDefault'); - element.siblings().get(0).click(); - expect($.Event.prototype.preventDefault).toHaveBeenCalled(); - }); - it('should clear the model', function() { - scope.foo = 'bar'; - scope.$apply(); - var element = $compile('')(scope); - element.siblings().get(0).click(); - expect(scope.foo).toBe(null); - }); + it('should proper DOM structure', function () { + scope.foo = 'bar'; + scope.$digest(); + var element = $compile('')(scope); + expect(element.parent().is('span')).toBe(true); + expect(element.next().is('a')).toBe(true); }); + }); + describe('clicking on the created anchor tag', function () { + it('should prevent the default action', function () { + var element = $compile('')(scope); + spyOn($.Event.prototype, 'preventDefault'); + element.siblings().get(0).click(); + expect($.Event.prototype.preventDefault).toHaveBeenCalled(); + }); + it('should clear the model and control value', function () { + scope.foo = 'bar'; + var element = $compile('')(scope); + scope.$digest(); + expect(element.val()).toBe('bar'); + element.next().click(); + expect(scope.foo).toBe(null); + expect(element.val()).toBe(''); + }); + }); }); \ No newline at end of file