Elegantly passing a “click event” through multiple callbacks

When a user who is not connected to the system clicks on the specified button, I want to stop the event, collect his message, collect his letter if I don’t have it, and then execute the event.

I want to do everything in javascript because it will be much easier.

This is how I execute it, and I have 2 questions:

  • Is there a more elegant way to make callbacks with multiple levels?
  • The way I fire the event at the end seems hacky. What could be the best way to do this?
  jQuery("a.callbacktesting").click(function(event){

    if (success==false){
      event.preventDefault();
      event.stopImmediatePropagation();
      authentication({endevent:event,followup:afterEmail},collectEmail, failFn);
    }
  });

  //1st level function
  function authentication(params, successFn, failFn){
      if (success=true){
        successFn(params,params.followup,failFn);
      }else{
       failFn();
      }      
  }

  //2nd level function
  function collectEmail(params, successFn, failFn){
      console.log("Collecting email");
      if (success=true){
        successFn(params);
      }else{
       failFn();
      };
  }

  //After everything is done, you want to execute this
  function afterEmail(params){
    jele=$(params.endevent.currentTarget)
    action=params.endevent.type
    jele.trigger(action);
  }
+5
source share
2 answers

What's up with this:

if (success = true) {

?

, "success" true, correct? , "success" true, :

if (success === true) {

3 .

, :

//After everything is done, you want to execute this
function afterEmail(event) {
    $(event.currentTarget).trigger(event.type);
}

//2nd level function
function collectEmail(event) {
    console.log("Collecting email");
    if (success === true){
        afterEmail(event);
    } else {
        failFn();
    }
}

//1st level function
function authentication(event) {
    if (success === true) {
        collectEmail(event);
    } else {
        failFn();
    }
}

jQuery("a.callbacktesting").click(function (event) {
    if (success === false) {
        event.preventDefault();
        event.stopImmediatePropagation();

        authentication(event);
    }
});

DOM, jQuery.trigger . , .

+1

click . , JS , . , . , . , .

0

All Articles