Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout binding with hammer.js

I'm trying to combine Hammer.js and knockout.js, but it seem something doesn't work.

I used this function:

var events = ['tap', 'doubletap', 'hold', 'rotate',
    'drag', 'dragstart', 'dragend', 'dragleft', 'dragright', 'dragup',
    'dragdown', 'transform', 'transformstart',
    'transformend', 'swipe', 'swipeleft', 'swiperight',
    'swipeup', 'swipedown', 'pinch', 'pinchin', 'pinchout'];

ko.utils.arrayForEach(events, function (eventName) {
    ko.bindingHandlers[eventName] = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var origParams = ko.utils.unwrapObservable(valueAccessor()),
                params = typeof(origParams) == 'function' ? {handler: origParams} : origParams,
                hammerObj = hammer(element, params.options),
                args = params.delegate ? [eventName, params.delegate, params.handler] : [eventName, params.handler]

            hammerObj.on.apply(hammerObj, args);
        }
    }


});

For example I'm trying to get drag event for every image that i have on the page so on my HTML I'm haveing that:

<div class="row-fluid" data-bind="foreach: {data: picturesArray}">
    <ul class="thumbnails" >
        <li class="img-item" data-bind="
            doubletap: img.doubletap.bind(img),
            drag: img.drag.bind(img)">                
            <img  class="one-img" 
              style="width: 100px; height: 100px;" data-bind="attr: { src: img.media.m }"/>
            </li>
        </ul>
    </div>

but it doesn't work. on my JS file, the bind methond doesn't called.

like image 737
user3488885 Avatar asked Apr 25 '14 10:04

user3488885


1 Answers

Here's how I solved it.

http://plnkr.co/edit/WJnPNsHQIKbbUu7oaD12?p=preview

Javascript

var events = ['tap', 'doubletap', 'hold', 'rotate',
'drag', 'dragstart', 'dragend', 'dragleft', 'dragright', 'dragup',
'dragdown', 'transform', 'transformstart',
'transformend', 'swipe', 'swipeleft', 'swiperight',
'swipeup', 'swipedown', 'pinch', 'pinchin', 'pinchout'];

ko.utils.arrayForEach(events, function (eventName) {
  ko.bindingHandlers[eventName] = {
      update: function(element, valueAccessor){
          var BindingContext = valueAccessor()[0];
          var EventToFire = valueAccessor()[1];

          var options = {
            dragLockToAxis: true,
            dragBlockHorizontal: true
          };

          var hammerTime = new Hammer(element, options);
          hammerTime.on(eventName, function(ev){ 
            //Fire the event with the item it was bound to.
            EventToFire(BindingContext);
          });
      }
  }

});

Html

<div data-bind="foreach: Elements">
  <!-- send yourself and the binding event to the "drag" handler -->
  <div class="square" data-bind="drag: [$data, $parent.Drag]">
    <div data-bind="text: Test"></div>
  </div>
</div>
like image 58
djbielejeski Avatar answered Sep 26 '22 00:09

djbielejeski