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

edited November 2, 2017 in Questions
I was having issues with getting a function to run after a model updateData function was called. See below for a the answer.

Comments

  • edited November 2, 2017
    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
    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!