jquery - Trying to understand DRY principles in Javascript -


i trying sharpen refactoring skills, have block of code have written has 2 methods similar, , trying wrap head around simplifying bloated code, suggestions welcome.

as can see 2 methods similar, , real difference url post to.

authenticatea : function( e ) {   var $this = $( e.target ).closest( '[data-fn]' )   ,   text = $this.text()   ,   = this;    $this.text( 'authenticating...' ).addclass("auth-button-disable")    $.ajax({     type : 'post',     url : '/a_authentications/update/',     data : { _method : 'put', sms_token : this.$el.find( '#sms-input' ).val() },     complete: function( xhr ) {        if ( xhr.status === 200 )         that.relocate();       else {         $this.text( text ).removeclass("auth-button-disable");         that.handleerror( xhr.status );       }     },     datatype : 'json'   }); },  authenticateb : function( e ) {   var $this = $( e.target ).closest( '[data-fn]' )   ,   text = $this.text()   ,   = this;    $this.text( 'authenticating...' ).addclass("auth-button-disable")    $.ajax({     type : 'post',     url : '/b_authentications/',     data : { otp : this.$el.find( '#b-input' ).val() },     complete: function( xhr ) {       if ( xhr.status === 200 )         that.relocate();       else {         $this.text( text ).removeclass("auth-button-disable");         that.handleerror( xhr.status )       }     },     datatype : 'json'   }); } 

i call methods click functions in events block:

'click [data-fn="authenticate-a"]' : 'authenticatea', 'click [data-fn="authenticate-b"]' : 'authenticateb' 

i think these refactored 1 method or 2 slimmer methods, i'm not sure start, again in advance.

you can have function generates functions:

generateauthfunction : function( authdetails) {   return function (e) {     var $this = $( e.target ).closest( '[data-fn]' )     ,   text = $this.text()     ,   = this;      $this.text( 'authenticating...' ).addclass("auth-button-disable")      $.ajax({       type : 'post',       url : authdetails.url,       data : authdetails.datafunc(this),       complete: function( xhr ) {          if ( xhr.status === 200 )           that.relocate();         else {           $this.text( text ).removeclass("auth-button-disable");           that.handleerror( xhr.status );         }       },       datatype : 'json'     });   }; } 

then generate with:

var authdetailsa = {   url :  '/a_authentications/update/',   datafunc : function (this) {     return { _method : 'put', sms_token : this.$el.find( '#sms-input' ).val() };   } }; var authdetailsb = {   url :  '/b_authentications/',   datafunc : function (this) {     return { otp : this.$el.find( '#b-input' ).val() }; }; authenticatea : generateauthfunction(authdetailsa); authenticateb : generateauthfunction(authdetailsb); 

you call before:

'click [data-fn="authenticate-a"]' : 'authenticatea', 'click [data-fn="authenticate-b"]' : 'authenticateb' 

i think might introduce unnecessary complexity, more dry.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -