Swipe action binding in knockoutjs

2019-04-28 17:29发布

I am using knockout as main framework in my application and it has to support tablets and mobile devices. As the framework is built on binding handlers I wonder how can custom binding to actions (like swipe and other device specific ones) be achieved, or maybe there is something like this done?

4条回答
老娘就宠你
2楼-- · 2019-04-28 17:59

There are no built in bindings for specific frameworks as knockout.js has no dependencies on any other framework. It should be a trivial task to convert your jQuery selector code to binding handlers referencing the link @niko provides above.

查看更多
三岁会撩人
3楼-- · 2019-04-28 18:06
  1. Create a bindingHandler. Here you go an example of a real project

    ko.bindingHandlers.swipeSections = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var elem = $(element);
        var params = valueAccessor().split('##');
    
        elem.unbind('swipe');
    
        elem.swipe({
            swipeLeft: function (event, direction, distance, duration, fingerCount) {
                //process
            },
            swipeRight: function (event, direction, distance, duration, fingerCount) {
                //process
            }
        });
    }
    

    };

  2. Using a swipe library: https://github.com/mattbryson/TouchSwipe-Jquery-Plugin

    script type="text/javascript" src="scripts/jquery.touchSwipe.js"
    
  3. Define the item's binding

    div id="myid" class="section" data-bind="swipeSections: 'leftPanel##rightPanel'"
    
查看更多
爷、活的狠高调
4楼-- · 2019-04-28 18:08

Probably too late, but here is a library that adds touch binding to knockoutjs: https://github.com/yaroslavya/knockouch

查看更多
狗以群分
5楼-- · 2019-04-28 18:17

I dont know if it still helps, but here is a pointer.

  1. Use a library like Hammer.js to get multi touch actions.
  2. Write a custom binding handler and call the knockout's default event binding. something like this for a swipe. (the author of the fiddle uses tap.js)

http://jsfiddle.net/snaptopixel/spByj/

now all you do in your html is

<button data-bind="tap:doSomething">Click Me</button>​

where doSomething is a function.

查看更多
登录 后发表回答