Extract ngTouch's fast click logic into jqLite's click event handler
This is a followup to issue #3130, since it's been closed.
Extracting the swipe logic to a service helps custom directives make use of such non-standard DOM events, and it seems like a nice solution. For swipes. Because swipes are events specific for touch-enabled devices.
But click/tap events happen on both desktop and touch-enabled devices, and there should be no additional code someone creating a custom directive needs to write in order to take advantage of the "fast click" implementation. If I'm using element.on('click', ...) on my custom directive, and the module ngTouch is included, fast clicks should just happen. As it stands now, the fast click implementation is contained in the ngTouch's ngClick replacement implementation, and there is no way directive creators can use such logic easily.
Wouldn't it be better to extract such logic into a jqLite's click event handler replacement?
I don't understand what this is asking for. ngTouch's ngClick fires an ordinary click event. All you should need to do to change from browser clicks to ngTouch fast clicks is to load angular-touch(.min).js and have your module depend on the ngTouch module.
Have you tried using element.on('click', ...)? It should be working, since the ngTouch ngClick handler fires that event. See here.
ngTouch's
ngClickfires an ordinary click event
Well, actually it does way more than that. ngTouch's ngClick triggers click listeners after if decides it was a valid click in a way that is faster than the browser's default logic. It does that by detecting touchstart, touchmove, touchcancel and touchend itself and determining when the user has tapped on something. But such special handling is implemented by the directive, so you only get that if ngClick is used.
That is no good for custom directives (i.e; widgets) that implement the click behavior internally though element.on('click', ...) and thus never make use of ngClick.
Do you see my point now? element.on('click', ...) will work, of course, but the browser's delay will still happen because ngClick was never used on the target element.
The code you pointed me to belongs to the directive. The directive needs to be used for such code to run. That's my point: maybe it would be better to extract it from the directive and put it into jqLite so every click (bound through ngClick or element.on('click', ...)) would be a fast click.
Ah, okay, I understand your point now. I suppose you can't use ngClick in the directive's template?
Is this a request to make some sort of service to register click handlers, akin to $swipe? One that could have a simple implementation in core which gets overridden by ngTouch?
To use ngCLick in the directive's template, the directive would have to expose a click handler to the scope. Doesn't seem like a nice solution because we'd be tempering with the user's scope. Making the directive request an isolate scope just for that also seems like an overkill, and there are all those gotchas when you isolate scopes and all, you know. Also, if the user does use ngClick on the widget, then you'd have to check for it in your directive so you can honor the user's handler as well as your own. I've done that, and it's ugly :)
Is this a request to make some sort of service to register click handlers, akin to $swipe? One that could have a simple implementation in core which gets overridden by ngTouch?
A request? Not really. I was kindda bringing it up for discussion so we all could come up with a good solution, if others do recognize the need.
Personally, I don't think a service is the way to go. We should just attach the click handler the normal way and the magic would happen if ngTouch module is used. Widget authors shouldn't need to implement a different logic to bind a click event. The way I see as the most transparent one is for ngTouch to augment jqLite's on/bind methods to apply the logic that ngClick does today.
But then, folks that use jQuery/zepto would be out of luck... hum...
What exactly is the difference when using fastClick as directive and below code as directive
app.directive("ngTap", function() { return function($scope, $element, $attributes) { var tapped; tapped = false; $element.bind("click", function() { if (!tapped) { return $scope.$apply($attributes["ngTap"]); } }); $element.bind("touchstart", function(event) { return tapped = true; }); $element.bind("touchmove", function(event) { tapped = false; return event.stopImmediatePropagation(); }); return $element.bind("touchend", function() { if (tapped) { return $scope.$apply($attributes["ngTap"]); } }); }; });
I would like to see it implemented as @diegovilar suggested, so we don't have to do extra logic and repeat the code inside each directive.
I mainy use AngularJS for Cordova/Phonegap applications and something like that would be appreciate it.
+1 for @diegovilar 's suggestion! (Or +10 if I may ;) I'm also looking this from perspective of Phonegap, and one particular case is driving me nuts:
My app is displaying data (mostly text) coming from various sources, including end-users. Often enough there are URLs within the text, either explicitly in http://domain.com -format or just in plaintext (like www.domain.com). I would of course like to enable these as clickable links that would open in phone's browser - anything else would be just bad UX.
To begin with, this looks like pretty straightforward: using regexps to find the URLs and create A-tags for them etc. BUT in order for Phonegap to open the links in external browser, the links MUST be handled via PG's JS-function - otherwise they will just open in place of the Phonegap app, completely overriding the app and providing no way back. I can of course easily do linkElement.bind('click', clickHandler)for the A-tags created via regexp, but the real problem arises when clicks on other elements on the same view are handled with ng-clicks on the template level.
It looks like those ng-clicks interfere severely with dynamically bound plain click-event, making it almost impossible to fire. This is only an issue on touch-based systems, so it clearly has to do with how the ng-click directive handles various touch-events. And as there is no way to create these links with ng-click on template level as they are based on dynamically loaded data, the only clean solution that I see would be to enable real ng-click-binding dynamically with bind() – just like @diegovilar is suggesting.
Sorry for the long rant, just wanted to give some context for the whole thing...
This is a good idea, just recently I needed to use the fastClick functionality, exactly like @diegovilar suggested, I wanted to listen on click/tap events so I was forced to use FastClick and attach to my created nodes, not a very clean solution I would say.
:+1:
We've been building a mobile app using Angular at Fetchnotes for the last few months, and have had to resort to re-evaluating the DOM using FastClick whenever a new page is loaded. One of the things we also do is utilize the same directives on our desktop web app (so writing two different versions of code utilizing element.on('click…' and element.on('touchstart'… would be non-trivial).
If we could decorate/shim jqLite's on method to utilize the speedy implementation present in ngTouch's ng-click implementation, then it would remove the need to perform an expensive re-evaluation of the DOM on page loads, and standardize how we handle interactions for all of our custom directives on both mobile and desktop.
jqLite is a subset of jQuery. because of that in v1.x we are not going to make any new changes to jqLite that would make it diverge from jQuery.
the proper workaround would be to either remove the delay via viewport metadata, use fastclick as described or create a decorator for jqLite that would be applied to angular.element to handle fast clicks.