We have two different lists that we are setting up some jslink client-side rendering files for their edit forms. Both csr files use the SP.Guid.newGuid()
function to id some DOM elements in the markup. One list worked just fine, but the other list always failed because the function was undefined.
We checked in the debugger and for the one list we noticed that none of the sp files — sp, sp.runtime, sp.core, sp.init, etc — had loaded. It seems like all that stuff should be loaded by the time we are rendering the form, right?
So my first question is, "Why would those files be loaded for one list's edit form, but not for another?"
Started looking for solutions and found this thread which shows using SP.SOD.executeOrDelayUntilScriptLoaded()
with the customContext.Templates.OnPreRender
. That solutions was for list views and maybe it is different, but on an Edit form the OnPreRender is called for every field. It might work, but seems like not the best solution to me.
Also I might need to require several other files. So I thought I could try a using load multiple, something like this:
function initForm(){
var ctx = {OnPreRender: myPreRender,
Templates:{
Fields:{
Title:{"EditForm":fancyField}
...
}
}}
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
}
RegisterSod('sp.js', '/_layouts/15/sp.js');
RegisterSod('sp.init.js', '/_layouts/15/sp.init.js');
RegisterSod('reputation.js', '/_layouts/15/reputation.js');
SP.SOD.loadMultiple(
['sp.js',
'sp.init.js',
'reputation.js'
],function(){initForm()}
)
But that didn't work because the form loaded and was all rendered before the csr code ran and registered the template overrides.
Then I found this thread which shows how you can specify multiple files to load in the jslink iteself, like so.
~sitecollection/_layouts/15/sp.init.js|~site/SiteAssets/csrListName.js
That does work, but I don't like it. It makes the csr file less portable to my mind. There is nothing self-contained in the csr file that shows the requirements for it to function and it requires going to edit the webpart and examine the jslink property to see what is required.
So my second question is this, "Is there a way to ensure the loading of required (perhaps multiple) SharePoint js files for a client-side rendering file within the file itself?"
SP.Guid.newGuid()
on chrome's console of both edit form pages and see if that works?