Mixing actions and snippets

  • 3
  • Question
  • Updated 4 years ago
  • Answered
So, I have been working on some situations where I am mixing actions from the action framework, and actual snipppets. My issue is along the lines of this one: https://community.skuidify.com/skuid/topics/block-ui-and-show-message-until-sforce-connection-remote....  I would imagine that most users would want each action to wait for the prior on to finish. Technically that would make every action a callback inside the previous one. I think that there should at least be an option on every action with something like "Don't run the next action until this one finishes". I think that would be a huge help to everyone using the action framework. Currently I have run into many issues with actions running too early.
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb

Posted 4 years ago

  • 3
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Hi Moshe,

Have you tried using the Deferred / Promise approach, as described in the post you referenced? 

With all standard actions that are asynchronous, such as "Save Models" and "Requery Models", the Action Framework does exactly this --- it waits until the asynchronous Save / Requery actions are finished before moving on to further Actions, if everything succeeds, or to On-Error Actions, if the Save/Requery has a problem. 

But if you are using Snippets, it's impossible for Skuid to "automatically" perform this sort of behavior, because Skuid doesn't know what kinds of asynchronous processing you are doing inside your function --- you as the Snippet writer are the one who has to decide at which points in your logic it is "okay" for the Action framework to proceed, and whether to "succeed", or to "fail" and run On-Error Actions instead. All that Skuid can provide are the tools for telling Skuid to "move on" or to "fail" --- which is exactly what the approach in the article above is providing. Your Snippet returns a Deferred Promise, and then you call either resolve() or reject() within the appropriate Callbacks in your code. If you call resolve(), Skuid knows to proceed with further actions, or if you call reject(), Skuid knows to run On-Error Actions.
(Edited)
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
OK perfect Zach, I was unsure of how exactly the whole Deffered/Promise thing worked (jQuery beginner). I also thought that it might only work with Irvins situation with a remote action. So basically if I'm writing a snippet that runs in middle of an action sequence, I can always do this:
var dfd = $.Deferred();
//do cool logic here
// assuming I have no on-error actions to attach a Callback to...
dfd.resolve(); 
return dfd.promise();
Will this work?
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Yes --- you can apply the Deferred/Promise approach to any asynchronous scenario. Here's some pseudo-code for the idea:

var dfd = $.Deferred();
someAsynchronousFunction({
    onSuccess: function(result) {
         // Run some logic to determine whether everything's cool
         if (result.isGoodToGo) {
            // Tell Skuid that we're okay to move on to the next action!
            dfd.resolve();
         } else {
            // Tell Skuid to run on-error actions
           dfd.reject();
         }
    },
    onError: function(result){
          // Tell Skuid to run on-error actions
         dfd.reject();
    }
});
return dfd.promise();