Note that there are some explanatory texts on larger screens.

plurals
  1. POSuccess handling in custom javascript function
    primarykey
    data
    text
    <p>If I make an ajax call, I can add success handling. I want to add similar logic to my custom functions.</p> <p>I have 6-10 custom functions that MUST be run sequentially or independently. They don't typically run independently so I have them daisy-chained now by calling the next function at the end of the previous but that is messy to read and does not allow for separate execution.</p> <p>I would love to have something like this:</p> <pre><code>function runall(){ runfirst().success( runsecond().success( runthird() )) } </code></pre> <p>I have had other situations were I would like to add <code>.success()</code> handling to a custom function, but this situation made it more important.</p> <p>If there is another way to force 6-10 functions to run synchronously, that could solve this problem, but I would also like to know how to add success handling to my custom functions.</p> <h3>I tried the following based on @lanzz's suggestion:</h3> <p>I added <code>.then()</code> to my function(s): </p> <pre><code>$bomImport.updateGridRow(rowId).then(function () { $bomImport.toggleSubGrid(rowId, false); }); var $bomImport = { updateGridRow: function (rowId) { $('#' + rowId + ' td[aria-describedby="bomImport_rev"]').html($("#mxRevTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_itemno"]').html($("#itemNoTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_used"]').html($("#usedTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_partSource"]').html($("#partSourceTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_partClass"]').html($("#partClassTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_partType"]').html($("#partTypeTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_partno"]').html($("#mxPnTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_descript"]').html($("#descTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_qty"]').html($("#qtyTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_custPartNo"]').html($("#custPartNoTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_crev"]').html($("#custRevTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_u_of_m"]').html($("#uomTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_warehouse"]').html($("#warehouseTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_standardCost"]').html($("#stdCostTxt").val()); $('#' + rowId + ' td[aria-describedby="bomImport_workCenter"]').html($("#wcTxt").val()); var defferred = new $.Deferred(); return defferred.promise(); }}; </code></pre> <p>The code correctly goes to the end of updateGridRow, gives no errors, but never gets back to call the second function.</p> <p>I also tried the following as was suggested @Anand:</p> <pre><code>workSheetSaveExit(rowId, isNew).save().updateRow().toggle(); function workSheetSaveExit(){ this.queue = new Queue; var self = this; self.queue.flush(this); } workSheetSaveExit.prototype = { save: function () { this.queue.add(function (self) { $bomImport.workSheetSave(rowId, isNew); }); return this; }, updateRow: function () { this.queue.add(function (self) { $bomImport.updateGridRow(rowId); }); return this; }, toggle: function () { this.queue.add(function (self) { $bomImport.toggleSubGrid(rowId, false); }); return this; } }; </code></pre> <p>Which didn't work.</p> <p><strong>Final Solution</strong><br/> For a great explanation of how to use deferred and make this work see here: <a href="http://www.erichynds.com/jquery/using-deferreds-in-jquery/" rel="nofollow">Using Deferred in jQuery</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload