Alex
Alex

Reputation: 7688

knockout click event

I have the following code:

<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);
    }
};

But I keep getting a button is not defined error. I'm trying to do an on('click') event with a parameter to determine latter what binding to initialize.

For example, when clicking on button('upload') I want to initialize the following 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"}
                ]
            });*/
        }
    };

Upvotes: 1

Views: 1625

Answers (1)

Vyacheslav Voronchuk
Vyacheslav Voronchuk

Reputation: 2463

You doing it wrong. You should create custom binding button which will be triggered on click event.

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

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

In view

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

Upvotes: 1

Related Questions