Hi Mark, it looks like you’re registering your snippet correctly in your static resource and connecting to it appropriately as a Javascript resource in your Skuid page.
The docs don’t have a specific example of calling a custom function stored in a static resource from an inline snippet, but I’d say keep doing what you’re doing since it works. I’ll check in with the internal team to see if I can get more information about best practices here.
In case you haven’t seen the docs on this, you can check them out here: https://docs.skuid.com/latest/en/skuid/javascript/resource-types/#static-resource.
As a general best practice, we recommend storing most JS as a static resource since it’s more efficient and improves the page’s load time. From Skuid and Code:
"If you’re consistently using a piece of code—such as a JavaScript snippet—if you’re building a component pack for your custom component, or if you’re an ISV developing a managed package, you should store that code within a Salesforce static resource
They can be stored in a source control repository _separate_ from your Skuid pages."
Hi Anna
Thanks for commenting on this one.
I would definitely appreciate any tips/examples on best practices if the internal team can provide any.
Mark,
I like to create an object to contain my snippets in a static resource, and then register them all with a quick loop. I think I picked the up from Zach at some point.
Here’s the basic concept:
Your Static Resource
(function (skuid){
//////////////////////////////////////////////
// Shortcuts & Global Variables //
//////////////////////////////////////////////
var $ = skuid.$,
$t = skuid.time,
$m = skuid.model,
mm = $m.Model;
//////////////////////////////////////////////
// Snippets //
//////////////////////////////////////////////
var snippets = {
‘myfirstsnippet’: function (param1, param2) {
// function code
},
‘mycustomfieldrenderer’: function (field, value) {
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field, value);
}
};
//////////////////////////////////////////////
// Register Snippets //
//////////////////////////////////////////////
$.each(snippets,function(name,func){ skuid.snippet.registerSnippet(name,func); });
})(skuid);
Then in the inline snippets on your pages you could call something like this:
skuid.snippet.getSnippet(‘myfirstsnippet’)(x,y);
Or you could simply type “mycustomfieldrenderer” into the snippet field for the custom field renderer.
Thanks Matt - that’s really useful.
I assume in my page I would still need to add a Static Resource javascript item to refer to this static resource?
So then I’d have 2 javascript items: the Static Resource and my regular inline snippet?