Call snippet from snippet

  • 3
  • Question
  • Updated 2 years ago
  • Answered
So I have a whole bunch of snippets out there, and I've been thinking about consolidating some of them into library methods that are contained in a static resource. Firstly are there any performance implications by including snippets in a resource? What are the best practices for doing this? Also I got the static resource working with register snippet, and I can get the snippet with getSnippet. I understand that I can call it with something like:

var i = skuid.snippet.getSnippet('getCorrectURLIfPortal');

but how can I set the arguments for the snippet? Do I pass them into the apply function?
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,686 Points 5k badge 2x thumb

Posted 5 years ago

  • 3
Photo of Zach McElrath

Zach McElrath, Employee

  • 53,702 Points 50k badge 2x thumb
Calling getSnippet returns you a reference to the Snippet, which is just a JavaScript function that you registered with Skuid. So once you get a reference to the Snippet, just call it like you would any other JavaScript function, passing in whatever arguments you want to pass, whether that's a single argument that's an object with lots of properties (this is what Skuid usually does internally), or a bunch of arguments comma separated, e.g.

// Get a reference to our stored Snippet
var getCorrectUrlSnippet = skuid.snippet.getSnippet('getCorrectURLIfPortal');

var line1 = 'My my my tis truly pumpkin pie time!';
var line2 = 'Or, rather, a delicious key lime!';
var line3 = 'Quoth the Raven, mine, mine, mine!';

// If your snippet expects comma-separated parameters...

// If your snippet expects an object containing parameters...
   line1: line1,
   line2: line2,
   line3: line3
Photo of Seth Vanderdrift

Seth Vanderdrift

  • 878 Points 500 badge 2x thumb
Snippets can have arguments?? I've searched for 'snippet parameters' and found this.

// If your snippet expects comma-separated parameters...getCorrectUrlSnippet(line1,line2,line3);
I would LOVE to have arguments.  I have 4 snippets which could be one snippet if I could pass a parameter from an action.  Ideally, the merge syntax would translate the action snippet name first, then call the snippet, but I've be very happy with just straight parameters.

Then if this would work with static resource snippets I wouldn't have the same snippet scattered all over pages.
Photo of Zach McElrath

Zach McElrath, Employee

  • 53,702 Points 50k badge 2x thumb
Seth, I'm assuming you're referring to wanting to pass parameters / arguments to Snippets called from a Step within the Action Framework? That a great idea.
Photo of Seth Vanderdrift

Seth Vanderdrift

  • 878 Points 500 badge 2x thumb
Yes, absolutely.  Snippet parameter would open many, many doors. I hadn't mentioned it because I figured it was on the 'future' list already.  

If you could use the merge-template with it, it would be STUPENDOUS!

I see two methods, roughtly corresponding to a by-value parameter:
This would obviously evaluate {{}} before passing that value as a parameter.  Now if that passes an object instead of the value...

And a  'by reference' parameter:

By reference is really a string, just need to be able to somehow 'de-reference' it in the snippet so it foo can update 'bar' easily.  Not sure what that would look like. Still new to skuid and snippets/javascript.

But with this, I could have my stock routines as static resources and call them from many pages.  My initial use case is for convenience buttons to increment a date field by xx number of days. (theres an action for that, but the 'Next N Days' is currently not working so you need snippets as a workaround.

Since JS is interperted (??), would it be possible to pass js code to a snippet as a parameter, that the snippet could run? Think similar to a sort routine where you pass a compare function.  Getting off into the weeds a bit... 
Photo of Pat Vachon

Pat Vachon, Champion

  • 44,426 Points 20k badge 2x thumb
Yup. I'm starting to get to this point where I need to snippets that can behave differently ever so slightly. Right now, I have to create and manage multiple versions of essentially the same code.
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,700 Points 4k badge 2x thumb
like... Like.... LIKEEEE Thanks Zazh!! Spelling it out help so much for a javascript noobie like me.