5
votes

I am using knockout.js. I have created a custom binding and applied it on anchor tag like this :

<a data-bind="custom : { param1 : 'text', param2: 'text' }">delete</a>

ko.bindingHandlers.custom = {
   init: function (element, valueAccessor, allBindingsAccessor) {
      alert("init");
   },
   update: function (element, valueAccessor, allBindingsAccessor) {
      alert("update");
   }
}

When i first load the page both the init and update functions called. But when i click on delete link update function is not call. I want to call update function of my custom binding whenever i click over the delete link. What i am doing wrong here ?

2

2 Answers

5
votes

The update function is only called initially when knockout applies the bindings and if any referenced observables are changed.

Your update function doesn't reference any observables and therefore doesn't get called again beyond the initial call.

If you want your function to be called when you click on it, you should use the click binding instead.

0
votes

You have to call a function on click of 'delete', then apply binding again in that function. Your problem will be solved. Because you have bind value when 1st time it loads, not on delete click.

<a data-bind="custom : { param1 : 'text', param2: 'text' }" 
   onclick='delete()'>delete</a>

<script type=javascript>
    function delete(){ ko.applyBindings(ko.bindingHandlers.custom);}
</script>