Skip to content
This repository has been archived by the owner on May 5, 2018. It is now read-only.

Commit

Permalink
Merge pull request #153 from angular-ui/issue149
Browse files Browse the repository at this point in the history
Fixes for the reset issue #149
  • Loading branch information
Christopher Hiller committed Aug 18, 2012
2 parents b2cd39e + 3fe1fc0 commit b4a2ee7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
25 changes: 14 additions & 11 deletions modules/directives/reset/reset.js
@@ -1,18 +1,21 @@

/** /**
* Add a clear button to form inputs to reset their value * 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 { return {
require:'ngModel', require:'ngModel',
link: function(scope, elm, attrs, ctrl) { link:function (scope, elm, attrs, ctrl) {
elm.wrap('<span class="ui-resetwrap" />').after('<a class="ui-reset" />').next().click(function(e){ var aElement = angular.element('<a class="ui-reset" />');
e.preventDefault(); elm.wrap('<span class="ui-resetwrap" />').after(aElement);
scope.$apply(function(){
// This lets you SET the value of the 'parsed' model aElement.bind('click', function (e) {
ctrl.$setViewValue(null); e.preventDefault();
}); scope.$apply(function () {
}); // This lets you SET the value of the 'parsed' model
} ctrl.$setViewValue(null);
ctrl.$render();
});
});
}
}; };
}]); }]);
71 changes: 37 additions & 34 deletions modules/directives/reset/test/resetSpec.js
@@ -1,41 +1,44 @@
/*global describe, beforeEach, module, inject, it, spyOn, expect, $ */ /*global describe, beforeEach, module, inject, it, spyOn, expect, $ */
describe('uiReset', function () { describe('uiReset', function () {
'use strict'; 'use strict';


var scope, $compile; var scope, $compile;
beforeEach(module('ui.directives')); beforeEach(module('ui.directives'));
beforeEach(inject(function (_$rootScope_, _$compile_, _$window_) { beforeEach(inject(function (_$rootScope_, _$compile_, _$window_) {
scope = _$rootScope_.$new(); scope = _$rootScope_.$new();
$compile = _$compile_; $compile = _$compile_;
})); }));


describe('compiling this directive', function() { describe('compiling this directive', function () {
it('should throw an error if we have no model defined', function() { it('should throw an error if we have no model defined', function () {
function compile() { function compile() {
$compile('<input type="text" ui-reset/>')(scope); $compile('<input type="text" ui-reset/>')(scope);
} }
expect(compile).toThrow(); expect(compile).toThrow();
});
it('should insert some crap into the DOM', function() {
scope.foo = 'bar';
scope.$apply();
var element = $compile('<input type="text" ui-reset ng-model="foo"/>')(scope);
expect(element.parent().is('span')).toBe(true);
});
}); });
describe('clicking on the created anchor tag', function() { it('should proper DOM structure', function () {
it('should prevent the default action', function() { scope.foo = 'bar';
var element = $compile('<input type="text" ui-reset ng-model="foo"/>')(scope); scope.$digest();
spyOn($.Event.prototype, 'preventDefault'); var element = $compile('<input type="text" ui-reset ng-model="foo"/>')(scope);
element.siblings().get(0).click(); expect(element.parent().is('span')).toBe(true);
expect($.Event.prototype.preventDefault).toHaveBeenCalled(); expect(element.next().is('a')).toBe(true);
});
it('should clear the model', function() {
scope.foo = 'bar';
scope.$apply();
var element = $compile('<input type="text" ui-reset ng-model="foo"/>')(scope);
element.siblings().get(0).click();
expect(scope.foo).toBe(null);
});
}); });
});
describe('clicking on the created anchor tag', function () {
it('should prevent the default action', function () {
var element = $compile('<input type="text" ui-reset ng-model="foo"/>')(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('<input type="text" ui-reset ng-model="foo"/>')(scope);
scope.$digest();
expect(element.val()).toBe('bar');
element.next().click();
expect(scope.foo).toBe(null);
expect(element.val()).toBe('');
});
});
}); });

0 comments on commit b4a2ee7

Please sign in to comment.