javascript - Break up into smaller functions MVC -


i have following function

 function registeruser(event) {     event.preventdefault();     let userdata = {         username: $('#formregister input[name=username]').val(),         password: $('#formregister input[name=passwd]').val()     };     $.ajax({         method: "post",         url: kinveybaseurl + "user/" + kinveyappkey + "/",         headers: kinveyappauthheaders,         data: json.stringify(userdata),         success: registersuccess,         error: handleajaxerror     });     function registersuccess(userinfo) {         saveauthinsession(userinfo);         showhidemenulinks();         listbooks();         showinfo('user registration successful.');      } } 

i'm trying in following way: direction controllers contains registerusercontroller.js file 1 function:

function registeruser(event) { event.preventdefault(); let userdata = {     username: $('#formregister input[name=username]').val(),     password: $('#formregister input[name=passwd]').val() }; registeruserajaxrequest(); function registersuccess(userinfo) {     saveauthinsession(userinfo);     showhidemenulinks();     listbooks();     showinfo('user registration successful.');    } } 

in other direction models contains registerusermodel.js file function:

function registeruserajaxrequest() { $.ajax({     method: "post",     url: kinveybaseurl + "user/" + kinveyappkey + "/",     headers: kinveyappauthheaders,     data: json.stringify(userdata),     success: registersuccess,     error: handleajaxerror }}); 

guess what, it's working if ajax request in regusteruser function. question is: how can break smaller functions , runs correctly ?

thanks in advance

that's solution i've created. im not sure practice.

that's register model:

  function registerusermodel(userdata) {    return $.ajax({         method: "post",         url: kinveybaseurl + "user/" + kinveyappkey + "/",         headers: kinveyappauthheaders,         data: json.stringify(userdata),         error: handleajaxerror     }); } 

and here register user controller

function registeruser(event) {     event.preventdefault();     let userdata = {         username: $('#formregister input[name=username]').val(),         password: $('#formregister input[name=passwd]').val()     };     registerusermodel(userdata).then(function (userinfo) {         saveauthinsession(userinfo);         showhidemenulinks();         showhomeview();     }) } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -