淘汰赛点击事件

时间:2012-12-28 11:34:43

标签: javascript jquery knockout.js

我有以下代码:

<div class="icon-upload" data-bind="click: button('upload') "> Upload </div>

ko.bindingHandlers.button = {
    init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext)
    {
        //alert('works');
        console.log(element);
    }
};

但我一直收到button is not defined错误。我正在尝试使用参数执行on('click')事件,以确定后者对初始化的绑定。

例如,点击button('upload')时,我想初始化以下binding

ko.bindingHandlers.image = {
        init: function (element, valueAccessor, allBindingsAccessor, context)
        {
            var value = ko.utils.unwrapObservable(valueAccessor()),
                $element = $(element);
                console.log($element)

            $element.html(value);

            /*$element.pluploadQueue({
                runtime: 'gears, browserplus, html5, flash, html4',
                max_file_size: '10mb',
                max_file_count: 10,
                chunk_size: '1mb',
                unique_names: true,
                multiple_queues: true,
                drop_element: true,
                dragdrop: true,
                filters : [
                    {title : "Image files", extensions : "jpg,gif,png"}
                ]
            });*/
        }
    };

1 个答案:

答案 0 :(得分:1)

你做错了。您应该创建将在button事件上触发的自定义绑定click

ko.bindingHandlers.button = {
   init: function (element) {
      $(element).click(function() {
          // Your logic
      });
   }

   update: function(element, valueAccessor, allBindingsAccessor) {
      switch(ko.utils.unwrapObservable(valueAccessor())) {
          case 'upload': ...
      }
   }
}

在视图中

<div class="icon-upload" data-bind="button: 'upload'"> Upload </div>