2012-12-28 92 views
1

我有以下代碼:淘汰賽click事件

<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

你做錯了。您應該創建自定義綁定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> 
+0

好吧,我想這樣嘗試比 – Alex

+0

我做到了另一種方式畢竟...但我相信你對接受的答案 – Alex