Vitor Durante
Vitor Durante

Reputation: 1063

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?

Upvotes: 0

Views: 117

Answers (1)

Mike Scotty
Mike Scotty

Reputation: 10782

$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.

Upvotes: 1

Related Questions