Skip to content

Instantly share code, notes, and snippets.

@kennethlynne
Created October 2, 2014 15:06
Show Gist options
  • Save kennethlynne/5a01fbfdcce766c8af75 to your computer and use it in GitHub Desktop.
Save kennethlynne/5a01fbfdcce766c8af75 to your computer and use it in GitHub Desktop.
A decorator to override uiSref to trigger state change on touchstart instead of click (to remove 300ms delay on touch screens)
'use strict';
// A decorator to override uiSref to trigger state change on touchstart if the element does not have disable-fastclick attribute
// It also logs the time between state changes
angular.module('app')
.config(function ($provide) {
$provide.decorator('uiSrefDirective', function ($delegate) {
var originalUiSref, originalUiSrefLink;
originalUiSref = $delegate[0];
originalUiSrefLink = originalUiSref.link;
$delegate[0].compile = function () {
return function (scope, element, attrs, uiSref) {
var originalBind = element.bind, enableFastClick = !angular.isDefined(attrs.disableFastclick);
element.bind = function (event, callback) {
originalBind.call(element, event === 'click' && enableFastClick ? 'touchstart mousedown' : event, function () {
callback.apply(element, arguments);
});
};
originalUiSrefLink.call($delegate, scope, element, attrs, uiSref);
};
};
return $delegate;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment