Can we make snippet to wait until called snippet execution completed

  • 1
  • Question
  • Updated 8 months ago
  • In Progress
if we are calling a snippet from another snippet like
var SaveQLIModels = skuid.snippet.getSnippet('SaveQLIModels');
SaveQLIModels ();
Can we make the current snippet to wait until SaveQLIModels snippet execution is completed.

I tried using
$.when(SaveQLIModels()).done(function(){
**Remaining logic**
})
but it not waiting until snippet SaveQLIModels() execution complete. Let me know what I am missing her.

Thanks in advance for the help.
Photo of Dinesh Ramanadham

Dinesh Ramanadham

  • 372 Points 250 badge 2x thumb

Posted 8 months ago

  • 1
Photo of Chandra V

Chandra V, Champion

  • 6,966 Points 5k badge 2x thumb
I think you need a call back function, which proceeds once your save is successful.

skuid.model.save([MyModel], {callback: function(result){
if (result.totalsuccess) {

// rest of your logic
  
} else {
//logic if the save fails
console.log(result.insertResults);
console.log(result.updateResults);
console.log(result.deleteResults);
return false;
}
}});
Photo of Dinesh Ramanadham

Dinesh Ramanadham

  • 372 Points 250 badge 2x thumb
Thanks Chandra for a quick reply.
Here in my scenario, I am calling another snippet. so this
skuid.snippet.getSnippet('SaveQLIModels'); 
will return a function and that function should have a callback to execute the remaining logic?
(Edited)
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,050 Points 10k badge 2x thumb
Dinesh, you might also be able to set up a Model Action on the model that's being saved. You can use "model saved" as the trigger, then set up an action sequence that runs the SaveQLIModels snippet. 
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,050 Points 10k badge 2x thumb
By the way, if your SaveQULIModels snippet is just saving other models, you can set up the same kind of model action sequence to do that declaratively, whenever the first model is saved.
Photo of Dinesh Ramanadham

Dinesh Ramanadham

  • 372 Points 250 badge 2x thumb
@Mark, yes initially models save was defined declaratively and after that we are calling a snippet. but unfortunately all those declarative actions are firing asynchronously. So some time before save of models completed snippet is firing and it is holding the old data. That is the main reason, we wrote snippet for models save and calling another snippet on success of models save.