How can I have a function wait until after a model update to run?

  • 1
  • Question
  • Updated 2 years ago
  • Doesn't Need an Answer
I was having issues with getting a function to run after a model updateData function was called. See below for a the answer.
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb

Posted 2 years ago

  • 1
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb
Thanks to Matt and Pat for your posts on this!

https://community.skuid.com/skuid/top...


The main line of code from the below sample that provides the solution is

$.when(initialUpdateOrOtherDeferredFunction()).done([printSuccess,doStuffAfterUpdateAttempt]).fail([printFailure,doStuffAfterUpdateAttempt]);

See below for the rest of the solution


$ = skuid.$;
try{
    
    //both done and fail function take a function or array of functions as parameters. I chose an array for both to print sucess state.
//https://api.jquery.com/jQuery.when/
//https://api.jquery.com/deferred.done/
//https://api.jquery.com/deferred.fail/
//"Since deferred.done() returns the deferred object, other methods of the deferred object can be chained to this one"
//this is why .fail can be appended to .done
    $.when(initialUpdateOrOtherDeferredFunction()).done([printSuccess,doStuffAfterUpdateAttempt]).fail([printFailure,doStuffAfterUpdateAttempt]);
    
}
catch(e){
    
    console.log(e);
}

function initialUpdateOrOtherDeferredFunction(){
var mod = skuid.model.getModel('modelName');
//do pre-processing stuff to your model here
//return the deferred promise
return mod.updateData();

}


function printFailure(){
 
    console.log('Failure');
    
}
function printSuccess(){
 
    console.log('Success');
    
}
function doStuffAfterUpdateAttempt(){
//do stuff
//do stuff
//repeat stuff https://www.youtube.com/watch?v=nt9c0UeYhFc
}
(Edited)