简体   繁体   中英

custom callback on jquery event

I am working with jquery and jstree

I have an event that triggers every time my tree changes:

$tree.jstree()
    .on("changed.jstree",  function(event, target) {
        //manipulate data
    });

It works perfect. I can access "this" (the tree), and also event and target. But, I am trying to define a custom callback. I tried something like this:

window.customCallback = (function(event, target) {
    //manipulate data
    //$(this).foo() manipulates the tree
    //event.type to access the event type
    //target.node to access the node
}(this));

So I can use:

$tree.jstree()
    .on("changed.jstree",  customCallback(event, target));

But it doesn't work. Could somebody help me out?

$tree.jstree().on("changed.jstree", customCallback(event, target));

What you're doing is setting the result of customCallback as callback handler.

What you want to do is set the function itself as callback handler:

var customCallback = function(event, target) {
// ...
};

$tree.jstree().on("changed.jstree", customCallback);

Notice the "missing" brackets - because brackets would invoke the function and we don't want that.

The parameters will be passed to the handler automatically.

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