javascript - own callback in jQuery, can't call secound callback function -


i have created plugin jquery , have found problem in plugin, when use times or more, callback function everytime use last callback function, ajax calls working shut , thats perfect.

now got problems when want call success callback function or error callback function.

(function ( $ ) { var setsettings = function(options,attrid) {     var settings = $.extend({         datatype: 'json',         timeout: 600,         alerttoolbar: true,         alertid : attrid,         alertclass : 'dialogtoolbar',         callback_error : null,         callback_success : null     }, options );      return settings; }  var gettoolbar = function(header, content, mode) {     if ( mode == 'error' )     {         $( document.createelement('div') )         .addclass('alerttopnav alerttopnav-error')         .append(             $( document.createelement('div') )             .append(                 $( document.createelement('strong') )                 .html( header )             )             .append(                 $( document.createelement('br') )             )             .append( content )         )         .appendto('#'+ settings.alertid )         .slidedown()         .delay(3000)         .slideup(800, function()         {             $(this).remove();         });     }     else if ( mode == 'success' )     {         $( document.createelement('div') )         .addclass('alerttopnav alerttopnav-success')         .append(             $( document.createelement('div') )             .append(                 $( document.createelement('strong') )                 .html( header )             )             .append(                 $( document.createelement('br') )             )             .append( content )         )         .appendto('#'+ settings.alertid )         .slidedown()         .delay(3000)         .slideup(800, function()         {             $(this).remove();         });     } } var functionid = 0;  $.fn.ajax_callback = function(call_url, call_data_array,call_settings) {     settings = setsettings(call_settings,$(this).attr('id'));      if (!$(this).hasclass("dialogtoolbar")) {         $(this).addclass('dialogtoolbar');     }      $.ajax({         type: 'post',         url: call_url,         crossdomain: true,         xhrfields: {withcredentials: true},         data: call_data_array,         datatype: settings.datatype,         timeout: settings.timeout,         beforesend: function(data) {             // somthing before sending         },         success: function(data) {             if(data.status==200) {                 if ( settings.callback_success != null ) {                     settings.callback_success(data);                 }                  if (settings.alerttoolbar == true ) {                     gettoolbar(data.success.header,data.success.msg,'success');                 }             } else {                  if(settings.callback_error != null) {                     settings.callback_error(data);                 }                  if (settings.alerttoolbar == true ) {                     gettoolbar(data.error.header,data.error.msg,'error');                 }             }         },         error: function(jqxhr, textstatus, errorthrown) {             // somthing on error         }     }); }  }( jquery )); 

i use code call plugin:

$('#dialogtoolbar').ajax_callback('json-path-1',{     'parms' : 'value' }, {     alerttoolbar: false,     callback_success: $.testobj.callback.getall.success,     callback_error: $.testobj.callback.getall.error }); 

and if use 1 more time change success , error callback , json path ofc.

$('#dialogtoolbar').ajax_callback('json-path-2',{     'parms' : 'value' }, {     alerttoolbar: false,     callback_success: $.testobj.callback.getallsecound.success,     callback_error: $.testobj.callback.getallsecound.error }); 

its success , error callback secound function take effect, have trying debug code can't find eny god explain on why happen.

hope there 1 out here there can explain me whats going worng , why plugin not working shut be.

you have create settings global variable in plugin, every call plugin override it.

var settings = setsettings(call_settings,$(this).attr('id')); 

instead need have local reference current settings object, create variable local function given above.

also have pass setting reference gettoolbar

var gettoolbar = function(settings, header, content, mode) { } ..... gettoolbar(settings, data.success.header,data.success.msg,'success'); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -