Call snippet from snippet

  • 3
  • Question
  • Updated 1 year 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');
i.apply();

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,646 Points 5k badge 2x thumb

Posted 4 years ago

  • 3
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Anyone know the answer... ?
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Sorry for the delay, Moshe - we'll get back to you tomorrow. 
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Hey Rob I just had to bump this up before I fell off the front page :). #Impatient
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
If I did that, the whole first page would be filled. *Smiling ear to ear*
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k 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...
getCorrectUrlSnippet(line1,line2,line3);

// If your snippet expects an object containing parameters...
getCorrectUrlSnippet({
   line1: line1,
   line2: line2,
   line3: line3
});
(Edited)
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.
(Edited)
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k 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:
fooSnippet({{$Model.CaseModel.data.0.bar}})
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:
fooSnippet('{{$Model.CaseModel.data.0.bar}}')

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... 
(Edited)
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 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,328 Points 4k badge 2x thumb
like... Like.... LIKEEEE Thanks Zazh!! Spelling it out help so much for a javascript noobie like me.
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Thanks!!!!
Photo of JD Bell

JD Bell, Senior Product Engineer

  • 2,996 Points 2k badge 2x thumb
To answer the first part of your question - there is no performance hit. In fact, your page will be (ever so slightly) more performant by putting your snippets in a static resource (since it reduces the overall size of your page and the browser can cache the static resource).
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Point of clarification:

If I need to pass values that are not in the (arguments) to a snippet that I've stored in a static resource, I will need to create an inline snippet on the page to pass that value to my stored snippet?
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Matt,  I don't think that woudl be required. Your stored snippett can access the full Skuid API to retrieve model and component information. 
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Haha. Yep!
Photo of Velvel Marasow

Velvel Marasow

  • 172 Points 100 badge 2x thumb
I just use this:
skuid.snippet.getSnippet('exampleSnippet')(arg1, arg2, arg3);
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Yes. But I believe we're looking to be able to include arguments for our snippets declaratively in the builder.
Photo of Sneha

Sneha

  • 624 Points 500 badge 2x thumb
Awesome that the snippets of page includes can be called!! :)
Photo of Arne-Per Heurberg

Arne-Per Heurberg

  • 1,336 Points 1k badge 2x thumb
SOOO helpful. just saying. Thanks!!