简体   繁体   中英

How can i use same private variable in 2 functions

So im having 2 functions. Problem is in styep_id variable. I know that i can just declare it in second functions, but then he wont take out data from first function. So the question is how i can use the same variable without lost data on him

PS It shouldn't be public variable, cos it wont work. It wont hold the data.

    function delete_estimate_position_type() {
        var estpt_tr_jqobj, estpt_action_links_td_jqobj, styep_id, authenticity_token, request_url, stya_id;

        styep_id = $(this).attr("styep_id");

        // Ja ir tikko kā pievienots, tad tikai izmetīsim ārā no DOM
        if (!styep_id == "") {
            estpt_action_links_td_jqobj = $(this).parent();
            estpt_tr_jqobj = estpt_action_links_td_jqobj.parent();
            stya_id = $("td.service-type-est-position-estimate-position-type-name>input.stya-id-for-styep", estpt_tr_jqobj).val();
            estpt_tr_jqobj.remove();
            show_stya_delete_link_if_possible(stya_id);
            remove_estpgt_if_has_no_estpt($(this).attr("estpgt_id"));
        }
}

And

function save_configuration(){
  var estpt_for_estpgt = "";
  // Pārbaudam vai visām tāmes pozīciju grupām ir norādītas tāmes pozīcijas
  $('.estpt-for-estpgt').each(function(){
    if ($(this).find('tr.action_record_tr').size() == 0){
      estpt_for_estpgt = this;
      return false;
    }
  })

  if (estpt_for_estpgt == "") {
    var form = $(this).closest('form');
    form.submit();
            // Dzēsīsim ārā no datu bāzes
            authenticity_token = $("#authenticity_token").val();
            request_url = "/service_type_est_positions/" + styep_id + "/destroy_from_service_type_config";

            $.post(request_url, { authenticity_token: authenticity_token}, process_service_type_est_position_delete, "json");
  } else {
    $.alerts.okButton = 'Labi';
    jError("Vismaz vienai Tāmes pozīciju grupai nav norādīta neviena Tāmes pozīcija!", "Kļūda");
  }

  return false;
}

function remove_estpgt_if_has_no_estpt(estpgt_id) {
    // Paskatīsimies vai eksistē kāda tāmes pozīcija
    if ($("#estpt_for_" + estpgt_id + ">tr:first").size() == 0) {
        $("#estpgt_" + estpgt_id).remove();
        $("#estpt_tr_for_" + estpgt_id).remove();
    }
}

call your second function within the first and pass the variable as an argument:

function delete_estimate_position_type() { 
   save_configuration(styep_id)
}

function save_configuration(id){
   request_url = "/service_type_est_positions/" + id + "/destroy_from_service_type_config";
}

You can use like this

 function delete_estimate_position_type() {
        var estpt_tr_jqobj, estpt_action_links_td_jqobj, styep_id, authenticity_token, request_url, stya_id;

        styep_id = $(this).attr("styep_id");

       // your other code goes here.....
       // Your variable pass as argument
            remove_estpgt_if_has_no_estpt($(this).attr("estpgt_id"),styep_id.val() );
        }
}

// Get it from argument.

function remove_estpgt_if_has_no_estpt(estpgt_id,styep_id ) {
    // Paskatīsimies vai eksistē kāda tāmes pozīcija
    if ($("#estpt_for_" + estpgt_id + ">tr:first").size() == 0) {
        $("#estpgt_" + estpgt_id).remove();
        $("#estpt_tr_for_" + estpgt_id).remove();
    }
}

Another option is set it in hidden field.

// html

<input type = "hidden" id="styep_id_newval" value="">

// End of html

function delete_estimate_position_type() {
        var estpt_tr_jqobj, estpt_action_links_td_jqobj, styep_id, authenticity_token, request_url, stya_id;

        styep_id = $(this).attr("styep_id");

       // your other code goes here.....
       $("#styep_id_newval").(styep_id.val());

       // Your variable pass as argument
            remove_estpgt_if_has_no_estpt($(this).attr("estpgt_id"),styep_id.val() );
        }
}

Now you can easily access code using anywhere.

$("styep_id_newval").val(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM