Call snippet from snippet

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');<br>i.apply();


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

Anyone know the answer… ?

Sorry for the delay, Moshe - we’ll get back to you tomorrow. 

Hey Rob I just had to bump this up before I fell off the front page :). #Impatient

If I did that, the whole first page would be filled. Smiling ear to ear

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
});


Thanks!!!

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).

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.

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.

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… 

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.

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?

Matt,  I don’t think that woudl be required. Your stored snippett can access the full Skuid API to retrieve model and component information. 

Haha. Yep!

like… Like… LIKEEEE Thanks Zazh!! Spelling it out help so much for a javascript noobie like me.

I just use this:

skuid.snippet.getSnippet('exampleSnippet')(arg1, arg2, arg3);

Yes. But I believe we’re looking to be able to include arguments for our snippets declaratively in the builder.

Awesome that the snippets of page includes can be called!! :slight_smile:

SOOO helpful. just saying. Thanks!!