1

我有以下代码:

<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"}
                ]
            });*/
        }
    };
4

1 回答 1

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>
于 2012-12-28T11:44:39.367 回答