Backbone.js click event doesn't work with touch

后端 未结 8 1809
-上瘾入骨i
-上瘾入骨i 2020-12-16 02:46
events: 
    \'click\' : \'select\'

When using this event on Mobile Safari the event gets triggered twice when touched. Is this a known bug or some

相关标签:
8条回答
  • 2020-12-16 03:05

    I have used Modernizr to detect the touch device and used following code and this worked for me.

    events :function(){ 
       return Modernizr.touch ? 
         {
             "touchstart #edit" : "openEdit",
         }: 
         {
             "click #edit" : "openEdit",
         }
     }
    
    0 讨论(0)
  • 2020-12-16 03:15

    Try this code:

    TouchView = Backbone.View.extend({
      events: function() {
        return MOBILE ? 
           {
             "touchstart": 'select'
           } : 
           {
             "mousedown": 'select'
           }
      }
    }
    

    See it in action: http://jsfiddle.net/dira/Ke2px/2/

    0 讨论(0)
  • 2020-12-16 03:16

    this worked for me.

    events:{
      'click #edit':'select',
      'touchstart #edit':'select'
    },
    select: function(e){
      e.stopPropagation();
      e.preventDefault();
      console.log('open upload dialog ', e.type);
    }
    

    now when you test this if the device is touch e.type should be touchstart and only fire once. Same for click on no-touch device. In case anybody is still looking for a simple solution for this.

    0 讨论(0)
  • 2020-12-16 03:18

    I just include the jquery touchpunch library and that's it.

    https://github.com/furf/jquery-ui-touch-punch

    0 讨论(0)
  • 2020-12-16 03:24

    Using coffeescript, I'd do the following, I don't ever find a reason to bring in modernizer when every mobile device these days is really a touch device, I mean when was the last time you had to really support something that didn't?

    window.isTouchDevice =  (/android|webos|iphone|ipod|ipad|blackberry|iemobile/i.test(navigator.userAgent.toLowerCase()) )
    
      events: ->
        for k, v of this when /click/.test(k) and isTouchDevice
          mobileKey = k.replace('click','touchstart')
          events[ mobileKey ] = v
          delete events[ k ]
        return events
    

    Coffeescript reads better for these type of list comprehensions imho.

    0 讨论(0)
  • 2020-12-16 03:25

    I'm not familiar with Backbone, but maybe try setting it conditionally?

    if ('ontouchstart' in document.documentElement) {
      // 'touchstart': 'select'
    } else {
      // 'click': 'select'
    }
    
    0 讨论(0)
提交回复
热议问题