简体   繁体   中英

Knockout change the click event

I'm trying to find a way to change the click event on all my elements. I want to do something like this but I think how KO binds the click event its actually attached to the element and so changing the function has no effect.

viewModel.clickEvent = function(item){
   logic
}

viewModel.clickEvent = newFunction;

<div data-bind="click: clickeEvent">MyButton</div>

I think that I need to use delegates but having a hard time figuring out how to do this. Could anyone post a basic example of how to do this with knockout?

If I have understood right. You could create a fake event handler that you can modify without modifying the actual event handler which is binded to the view.

var viewModel = {
    clickEvent : function(item){
        if(this.changableClickEvent)
            this.changableClickEvent(item);
    },
    changableClickEvent : null
}

viewModel.changableClickEvent = function(){
    // logic
    alert('logic');
}

ko.applyBindings(viewModel);

See fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM