2012-05-04 36 views
6

我在我的应用程序中使用基诺作为主要框架,它必须支持平板电脑和移动设备。由于该框架是基于绑定处理程序构建的,我不知道如何实现自定义绑定到动作(如滑动和其他设备特定的动作),或者可能有类似这样的事情?在knockoutjs中滑动操作绑定

+0

你可以添加自己的绑定:http://knockoutjs.com/ documentation/custom-bindings.html – Niko

+0

是的,我知道。但我正在寻找的东西是一些很好的内置在滑动,点击和其他触摸事件绑定(因为现在我正在初始化它在文档开始与jQuery选择器并手动绑定这些动作) –

回答

0

由于knockout.js对任何其他框架没有依赖关系,因此没有针对特定框架的内置绑定。将jQuery选择器代码转换为引用@niko上面提供的链接处理程序应该是一件简单的任务。

0

我不知道它是否仍然有帮助,但这里是一个指针。

  1. 使用类似Hammer.js的库来获得多点触摸操作。
  2. 编写自定义绑定处理程序并调用淘汰赛的默认事件绑定。像这样的滑动。 (小提琴的作者使用tap.js)

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

现在你在你的HTML做的是

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

其中DoSomething的是一个函数。

1
  1. 创建bindingHandler。在这里你去一个真实项目的例子

    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. 使用刷卡库:https://github.com/mattbryson/TouchSwipe-Jquery-Plugin

    script type="text/javascript" src="scripts/jquery.touchSwipe.js" 
    
  3. 定义项的结合

    div id="myid" class="section" data-bind="swipeSections: 'leftPanel##rightPanel'"