From 2a30a02f015dd54846bb62d1f05e82b3cf76ef9f Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 27 Jul 2010 15:54:50 -0700 Subject: fix preventDefault for events --- example/temp.html | 7 ++++++- scenario/widgets.html | 8 ++++---- src/directives.js | 4 ++-- src/jqLite.js | 15 ++++----------- src/widgets.js | 5 +++-- test/directivesSpec.js | 14 ++++++++------ 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/example/temp.html b/example/temp.html index 337f7fba..f21d3f5c 100644 --- a/example/temp.html +++ b/example/temp.html @@ -1,10 +1,15 @@ + - Hello {{'World'}}! +
+ outter +
inner
+ link +
diff --git a/scenario/widgets.html b/scenario/widgets.html index 2626843d..d5285ea6 100644 --- a/scenario/widgets.html +++ b/scenario/widgets.html @@ -72,12 +72,12 @@ Buttons - ng-change
ng:click + ng:change
ng:click
-
-
-
+
+
+
action
diff --git a/src/directives.js b/src/directives.js index ffe37890..9aadbd11 100644 --- a/src/directives.js +++ b/src/directives.js @@ -199,10 +199,10 @@ angularWidget("@ng:repeat", function(expression, element){ angularDirective("ng:click", function(expression, element){ return function(element){ var self = this; - element.bind('click', function(){ + element.bind('click', function(event){ self.$tryEval(expression, element); self.$root.$eval(); - return false; + event.preventDefault(); }); }; }); diff --git a/src/jqLite.js b/src/jqLite.js index cff9ae00..26ca6dea 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -104,19 +104,12 @@ JQLite.prototype = { eventHandler = bind[type]; if (!eventHandler) { bind[type] = eventHandler = function(event) { - var bubbleEvent = false; + if (!event.preventDefault) { + event.returnValue = false; + } foreach(eventHandler.fns, function(fn){ - bubbleEvent = bubbleEvent || fn.call(self, event); + fn.call(self, event); }); - if (!bubbleEvent) { - if (msie) { - event.returnValue = false; - event.cancelBubble = true; - } else { - event.preventDefault(); - event.stopPropagation(); - } - } }; eventHandler.fns = []; addEventListener(element, type, eventHandler); diff --git a/src/widgets.js b/src/widgets.js index 5f0fcf7c..87a302fa 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -198,14 +198,15 @@ function inputWidget(events, modelAccessor, viewAccessor, initFn) { this.$eval(element.attr('ng:init')||''); // Don't register a handler if we are a button (noopAccessor) and there is no action if (action || modelAccessor !== noopAccessor) { - element.bind(events, function(){ + element.bind(events, function(event){ model.set(view.get()); lastValue = model.get(); scope.$tryEval(action, element); scope.$root.$eval(); // if we have noop initFn than we are just a button, // therefore we want to prevent default action - return initFn != noop; + if(initFn == noop) + event.preventDefault(); }); } view.set(lastValue = model.get()); diff --git a/test/directivesSpec.js b/test/directivesSpec.js index 8a7da41d..278f9c4c 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -160,13 +160,15 @@ describe("directives", function(){ expect(scope.$get('count')).toEqual(1); }); - it('should ng:click', function(){ - var scope = compile('
'); - scope.$eval(); - expect(scope.$get('clicked')).toBeFalsy(); + describe('ng:click', function(){ + it('should fire event', function(){ + var scope = compile('
'); + scope.$eval(); + expect(scope.$get('clicked')).toBeFalsy(); - element.trigger('click'); - expect(scope.$get('clicked')).toEqual(true); + element.trigger('click'); + expect(scope.$get('clicked')).toEqual(true); + }); }); it('should ng:class', function(){ -- cgit v1.2.3