This small JS library helps you to boot JS with dynamic loading JavaScripts and HTML. It assists in loading these and waiting till they are loaded.
Try / discover it by opening demos/index.html
The core goal is that no matter which parts are loaded in what order, no errors occur and in the end, things will just load. To make this happen, you need two things
- to load the turnOn scripts (in 2sxc, a temporary solution is using
Edit.Enable(turnOn: true)
but this may still change) - add any html-tag containing a
turn-on
attribute with the configuration
Examples
<!-- Just wait for window.myObject.start to exist, then run -->
<span turn-on='{ "run": "window.myObject.start()" }' />
<!-- Also include more data which will be passed to the run -->
<span turn-on='{ "run": "window.myObject.start()", "data": { "id": 27 } }' />
<span turn-on='{ "run": "window.myObject.start()", "data": { "module": @CmsContext.Module.Id } }' />
<!-- debug the data passed in to see if the data is as expected -->
<turnOn turn-on='{ "run": "window.turnOn.dump()", "data": { "module": @CmsContext.Module.Id } }' />
<!-- also wait for $ to exist (jQuery) -->
<span turn-on='{ "require": "window.$", "run": "window.myObject.start()" }' />
<!-- wait for jquery and some deeper object -->
<span turn-on='{ "require": ["window.$", "window.someObject.subObj", "window.something.isReady()"], "run": "window.myObject.start()" }' />
- property
run
as of now must always start withwindow
and end with()
turnOn will verify that the parts leading up to the object exist before it's run. - property
await
is a string or string[]. It can just be a deep object or a function. Must always start withwindow
If it's a function name, it will be called and only regarded as ready if the result istrue
.
No matter if function or name, turnOn will carefully test/watch each part to see if it exists. - property
args
would pass an array of arguments to the function new v0.3.0 - property
data
passes one object to the run function - this is the classic method, but only works with functions that expect a single object
- As turnOn is working, the
turn-on
attribute is updated so you can watch a lot of what's happening - You can always set
window.debugTurnOn = true;
to see a lot of logs. - You can also use
<span turn-on='{ "run": "window.myObject.start()", debug: true }' />
to see more details for one specific turnOn
The solution is written in Typescript and is plain vanilla, no other dependencies used.
- Create NPM package
- Ensure NPM package also has type definitions
- add feature to get url from CDN etc. - kind of like loadJs
- possibly also get css from a CDN?
- create a special
custom-element
eg.<turn-on>
which will be replaced with a span and the turn-on will be executed. This could be faster than the current Mutex-based solution.
Notes to follow up
https://gist.github.com/james2doyle/28a59f8692cec6f334773007b31a1523
Because of CSP etc. we're considering a model like this
or
This would then kick off run once it's available. The call would then include a prepared object with various data to make the run do a better job. It would probably do this:
- put the original turn-on into turn-on-raw
- parse the turn-on and place the parsed object incl. progress etc. into turn-on
The object handed into the run would contain a reference to the tag what had this, so additional data could be attache do that tag for intialization. The object is probably
{
turnOn: object, //the current turn-on object
tag: HtmlElement, // the html tag which started the turn-on and might contain extra configuration
}
- v0.1.1 initial version
- v0.1.2 corrected so that
tag
no the context is the real html tag - v0.2.0 changing
awaits
in html tag to berequire
because it's a reserved term in C# and most turnOn is made in C# note: we never bumped the version that's shown, so v0.2.0 still shows v0.1.2 - v0.3.0 adding
args
variant with optionaladdContext