3

Is there built-in on call function event in JavaScript / jQuery ?

I mean something on('call' , function(){})

I use this custom event meanwhile:

function setCookie(name, value, days) {
    // set ...
    $(document).trigger( "setCookieEvent", [ name ] );
}
function cookieController()
{
    $(document).on( "setCookieEvent", function( event, name ) {
        //setCookie name
    });
}
cookieController();
7
  • 1
    Not sure what you mean. You want an event handler called when a different function is invoked? Commented Jan 10, 2016 at 0:39
  • 1
    no... but, what function do you want to have the event on. you can always create a decorator for the called function. Commented Jan 10, 2016 at 0:41
  • @squint Yes, every time function setCookie() called. Commented Jan 10, 2016 at 0:41
  • 1
    Assuming you can't simply add the call inside setCookie, you could cache the function and then replace it with one that invokes your new function as well as the cached one. But if you needed to do this with various handlers that need to be bound and unbound, you'd need to create your own mini event system, which isn't too hard. Commented Jan 10, 2016 at 0:47
  • 2
    No, but there's always monkey-patching. If it's your implementation, you could also add a custom event handler. Commented Jan 10, 2016 at 0:48

2 Answers 2

0

As said in the comments above, you could decorate the original function with some new functionality. You are essentially saving a reference to the original function, and rewriting the original function to include some new functionality.

function setCookie( name, value, days ) {
  console.log('setCookieCalled ' + name, value, days );
}

setCookie('[before decoration]', 'foo', 1 );

// rewrite the setCookie function
setCookie = decorate(setCookie, function( name, value, days ){
  console.log('decorated setCookie ' + name, value, days );
});

// this will modify the original function. and return a new function that decorates the old.
function decorate(originalFunction, decorator) {
  return function( /* arguments */ ) {
    console.log('called decorator ' + arguments[0] );
    // call the decorator and the original function
    decorator.apply( this, arguments );
    originalFunction.apply( this, arguments );
  }
}

setCookie('[after decoration]', 'bar', 2 );
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

You should use jQuery's event object:

function setCookie(name, value, days) {
    // set ...
    $.event.trigger({ type: "setCookieEvent", name: name, value: value });
}

function cookieController() {
  $(document).on("setCookieEvent", function(event) {
    document.write("Cookie " + event.name + " created with value " + event.value); 
  });
}


cookieController();

setCookie("cookie-name", "cookie-value", 100);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.