Lets say, I have custom-dialog.js
that depends on custom-notifier.js
and this one depends on sp.js
. What's the best practises to write dependencies inside mine custom .js
files?
1 Answer
You can use the great SP.SOD to both register dependencies for your files, but also for making sure they load in the correct order.
Here is a great guide to get started,
also here some from MSDN.
The easiest way is to, in the file that the other one is dependent on write
SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("FirstScriptFile.js")
and then when you reference your scripts:
RegisterSod('FirstScriptFile.js', '/_layouts/FirstScriptFile.js');
RegisterSod('SecondScriptFile.js', '/_layouts/SecondScriptFile.js');
RegisterSodDep('SecondScriptFile.js', 'FirstScriptFile.js');
the first parameter in the last one being the dependent file, and the second the dependency file.