| How do i return a value from a closure function?
|
nelsonm

msg:4459279 | 3:51 am on May 30, 2012 (gmt 0) | How do i get this closure function "getTaxRate" to return "taxrate" so i can use it in the parent function "recalcTotals"? // get tax rate. closure function. function getTaxRate(){ return (function(sdata) { var response = JSON.parse(sdata); var row = response.rows['0']; taxrate = row['cell']['4']; return taxrate; }); } function recalcTotals(){ var subtotal = 0.00, salestax = 0.00, taxrate = 0.00, surcharge = 0.00, salestotal = 0.00; var taxEnabled = true, data, sdata; // get each item cost and calculate subtotal. $('#workorder-form #sa-table-1 tbody tr input[name="wi-Cost"]').each(function (){if($(this).val() != ''){subtotal += parseFloat($(this).val());}}); // calculate surcharge. if(subtotal == 0.00){surcharge = 0.00;}else if(subtotal <= 100.00){surcharge = 5.00;}else{surcharge = subtotal * 0.06;} // gas surcharge calculation. data = {'_search':true, filters: '{"groupOp":"AND","rules":[{"field":"st.StateAbbrev","op":"eq","data":"'+$('#workorder-form select#cu-STID option:selected').text()+'"}]}', page:1, rows:40, searchField:'', searchOper:'', searchString:'', sidx:'st.StateAbbrev', sord:'asc'}; if(taxEnabled) {$.post('php/statetab-script.php', data, getTaxRate());} subtotal = (subtotal).toFixed(2); salestax = (parseFloat(subtotal) * (parseFloat(taxrate) / 100)).toFixed(2); taxrate = parseFloat(taxrate).toFixed(2); surcharge = (surcharge).toFixed(2); salestotal = (parseFloat(subtotal) + parseFloat(salestax) + parseFloat(surcharge)).toFixed(2); // fill sales total fields. set all totals to 2 decimal places. $('#workorder-form input#wo-SubTotal' ).val(subtotal); $('#workorder-form input#wo-SalesTax' ).val(salestax); $('#workorder-form label#wo-TaxRate' ).text(taxrate+'%'); $('#workorder-form input#wo-SurCharge' ).val(surcharge); $('#workorder-form input#wo-SalesTotal').val(salestotal); } |
|
|
daveVk

msg:4459313 | 6:58 am on May 30, 2012 (gmt 0) | function recalcTotals(){ var subtotal = 0.00, salestax = 0.00, taxrate = 0.00, surcharge = ... ... {surcharge = 5.00;}else{surcharge = subtotal * 0.06;} // gas surcharge calculation. // code to do once taxrate known var updateForm = function() { subtotal = (subtotal).toFixed(2); ... $('#workorder-form input#wo-SalesTotal').val(salestotal); }; if(taxEnabled) { // get taxrate and update form var data = ... $.post('php/statetab-script.php', data, function(sdata) { ... get taxrate code ... updateForm(); } );} } else {updateForm();} // dont get taxrate just update form }
|
nelsonm

msg:4459362 | 10:04 am on May 30, 2012 (gmt 0) | I tried everything but your idea... I'll try your method in a few hours and let you know. thanks.
|
nelsonm

msg:4459402 | 12:41 pm on May 30, 2012 (gmt 0) | It worked! thanks a lot. BTW, it was said on another post that i should do calculations on the server side because toFixed may give different results in different browsers. Do you agree with that? In any case, i do agree that you should hold off using toFixed until your ready to display the data. One more thing... assigning the anonymous function to variable "updateForm" does not create a closure - right? I think calling "updateForm" from the post method callback function creates closure - am i correct or am i still hopelessly lost on the concept of closures? [edited by: nelsonm at 1:25 pm (utc) on May 30, 2012]
|
nelsonm

msg:4459418 | 1:16 pm on May 30, 2012 (gmt 0) | Thanks to you and Fotiman... I was able to get my code not only working but working better. Here is the code you helped get working and improve: function recalcTotals(){ var subtotal = 0.00, salestax = 0.00, taxrate = 0.00, surcharge = 0.00, salestotal = 0.00; var taxEnabled = true, data, sdata; // get each item cost and calculate subtotal. $('#workorder-form #sa-table-1 tbody tr input[name="wi-Cost"]').each(function (){if($(this).val() != ''){subtotal += parseFloat($(this).val());}}); // calculate surcharge. if(subtotal == 0.00){surcharge = 0.00;}else if(subtotal <= 100.00){surcharge = 5.00;}else{surcharge = subtotal * 0.06;} // gas surcharge calculation. var updateForm = function() { salestax = subtotal * (taxrate / 100); salestotal = subtotal + salestax + surcharge; // fill sales total fields. set all totals to 2 decimal places. $('#workorder-form input#wo-SubTotal' ).val(subtotal.toFixed(2)); $('#workorder-form input#wo-SalesTax' ).val(salestax.toFixed(2)); $('#workorder-form label#wo-TaxRate' ).text(taxrate.toFixed(2)+'%'); $('#workorder-form input#wo-SurCharge' ).val(surcharge.toFixed(2)); $('#workorder-form input#wo-SalesTotal').val(salestotal.toFixed(2)); }; if(taxEnabled) { data = {'_search':true, filters: '{"groupOp":"AND","rules":[{"field":"st.StateAbbrev","op":"eq","data":"'+$('#workorder-form select#cu-STID option:selected').text()+'"}]}', page:1, rows:40, searchField:'', searchOper:'', searchString:'', sidx:'st.StateAbbrev', sord:'asc'}; $.post('php/statetab-script.php', data, function(sdata){ var response = JSON.parse(sdata); var row = response.rows['0']; taxrate = parseFloat(row['cell']['4']); updateForm(); }); }else{ updateForm(); } } |
| again, thanks a lot.
|
daveVk

msg:4459705 | 2:45 am on May 31, 2012 (gmt 0) | No idea about toFixed, but delaying till just prior to display is good idea. Yes I think, refering to updateForm(or any other local data), creates the closure. On exit of recalcTotals, updateForm has one user, and is not disposed of as would normally be the case, also data updateForm references is not disposed of.
|
|
|