watusi / jquery-mobile-iscrollview

JQuery Mobile widget plug-in for easy use of the iScroll javascript scroller.
407 stars 183 forks source link

jQuery event bindings crash on touch devices (patch) #127

Open alebedev opened 11 years ago

alebedev commented 11 years ago

Option to bind events via jQuery is great and significantly simplifies customizing iscroll behavior. There's, however, crash bug on touch devices caused by lack of TouchEvent attributes on jQuery events.

I fixed this by making change from patch below. Please consider including it into default implementation.

--- a/client/src/js/libs/jquery.mobile.iscrollview.js   Tue Oct 22 13:52:21 2013 +0400
+++ b/client/src/js/libs/jquery.mobile.iscrollview.js   Tue Oct 22 14:19:14 2013 +0400
@@ -236,6 +236,12 @@
       var jqEvents = this.iscrollview.options.bindIscrollUsingJqueryEvents,
           then;
       then = this.iscrollview._logIscrollEvent("iScroll.handleEvent", e);
+      // Copy touches to jQuery event
+      if (jqEvents) {
+        e.touches = e.originalEvent.touches;
+        e.changedTouches = e.originalEvent.changedTouches;
+        e.targetTouches = e.originalEvent.targetTouches;
+      }
       // If jQuery mouseleave, make iScroll think we are handling a mouseout event
       if (jqEvents && e.type === "mouseleave") {
         e.type = "mouseout";
alebedev commented 11 years ago

Small fix for attached code, in cases when originalEvent is undefined:

      // Copy touches to jQuery event
      if (jqEvents) {
        if (e.originalEvent) {
          e.touches = e.originalEvent.touches;
          e.changedTouches = e.originalEvent.changedTouches;
          e.targetTouches = e.originalEvent.targetTouches;
        } else {
          e.touches = e.changedTouches = e.targetTouches = [];
        }
      }
jtara commented 11 years ago

Thank you - jQuery event binding is an experimental feature, and not enabled by default because I know it has bugs. I wanted to leave the code in so that people could experiment and fix the bugs, so it seems that worked. ;)