While a few people have provided some good info regarding the usage of the two common commands for pre-loading registered and unregistered scripts, no one has addressed the various challenges in actually getting them to work as advertised.
Having implemented hundreds of SharePoint customisations - and particularly those that rely on SharePoint to complete it's AJAX calls - I can tell you that most of the official methods invariably fail...often without reason. What's most frustrating is that the code will work sometimes, in some browsers, on some OSs - sometimes for days! :)
The most affective method I have found to date to ensure near 100% success is to make certain that any calls to SP.SOD methods are encapsulated within the $(document).ready() function.
Moreover, that any Template Overrides (CSR) methods are also called within the anonymous function of executeFunc.
A simple example to illustrate this by overriding a document's CallOut window footer and inserting a new action to "Send Link By Email":
$(document).ready(function() {
// Run this after callout.js has loaded.
SP.SOD.executeFunc("callout.js", "Callout", function () {
var itemCtx = {};
itemCtx.Templates = {};
itemCtx.BaseViewID = 'Callout';
// Define the list template type
itemCtx.ListTemplateType = 101;
// Override the default footer
itemCtx.Templates.Footer = function (itemCtx) {
// context, custom action function, show the ECB menu (boolean)
return CalloutRenderFooterTemplate(itemCtx, AddCustomAction, true);
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);
});
});
// Add your Custom Action
function AddCustomAction (renderCtx, calloutActionMenu) {
calloutActionMenu.addAction (new CalloutAction ({
text: "Send Link By Email",
tooltip: 'Click to create a new email message containining a link to this document.',
onClickCallback: function() { sendMailLink(); }
}));
// Re-render the default actions
CalloutOnPostRenderTemplate(renderCtx, calloutActionMenu);
}
/* Callback function for Custom Action */
function sendMailLink() {
var itemUrl = "<" + _spPageContextInfo.siteAbsoluteUrl + currentItemFileUrl + ">";
var itemName = currentItemFileUrl.split("/").pop();
window.location.href =
"mailto:%20?subject=Link to: "
+ itemName
+ "&body=Here's a link to '" + itemName + "':%0D%0A"
+ itemUrl + "%0D%0A%0D%0A";
return false;
}
Hope this example helps others.