just.Define(idnullable, dependencyIDs, value)
Define a value after all dependencies became available.
Parameters:
Name | Type | Description |
---|---|---|
id [nullable] | string | The module id. |
dependencyIDs | string[] or string | Required module ids. |
value | Any | The module value. |
Example
// https://some.cdn/js/just.js
window.just = {'Define': function () {}};
// index.html
< !DOCTYPE html>
< html data-just-Define='{"main": "/js/main.js"}'>
< head>
< title>Test</title>
< script src='https://some.cdn/js/just.js' async></script>
< /head>
< body>
< /body>
< /html>
// /js/main.js
just.Define.configure({
'globals': {
// Set justJs to window.just
'justJs': function () { return just; }
},
'urls': {
// Load "/css/index.css" when "index.css" is required.
'index.css': '/css/index.css'
},
'nonScripts': {
// Call when "main.html" is required.
'main.html': function () { return '<main></main>'; }
}
});
// Load when document, justJs and index.css are ready:
just.Define('main', ['justJs', 'index.css'], function (j) {
if (j.supportsTouch()) {
j.Define('mobile', 'https://example/m');
return;
}
j.Define('non-mobile', ['main.html']);
});
// Call only if j.supportsTouch()
just.Define(['mobile'], function (url) {
window.redirect = url;
});
// Call when main.html is ready.
just.Define(['non-mobile'], function (html) {
document.body.innerHTML = html;
});
Members
globals :object.<just.Define~id, *>
A writable object literal that contains all the values that will be defined when required.
Type:
object.<just.Define~id, Any>
Examples
// index.js
just.Define.globals['just'] = 1;
just.Define('index', ['just'], function (just) {
// just === 1; > true
});
// https://some.cdn/js/just.js
window.just = {Define: 1};
// main.js
just.Define.globals['JustJs'] = function () { return just; };
just.Define.urls['JustJs'] = 'https://some.cdn/js/just.js';
just.Define('main', ['JustJs'], function (just) {
// just === {Define: 1};
});
// https://some.cdn/js/just.js
window.just = {Define: 1};
// index.html
<script src='https://some.cdn/js/just.js'
data-just-Define='{"JustJs": "[src]"}' async></script>
// main.js
if ('just' in window) { just.Define('JustJs', just); }
else { just.Define.globals['JustJs'] = function () { return just; }; }
just.Define(['JustJs'], function (just) {
// just === {Define: 1};
});
nonScripts :object.<just.Define~id, *>
A writable object literal that contains values for non script resources, like css. Since Define won't check for file contents when loads a new file, you must add the value here.
Type:
object.<just.Define~id, Any>
Example
just.Define.nonScripts['/css/index.css'] = function () {};
just.Define('load css', ['/css/index.css'], function (css) {
// by default, `css` is an HTMLElement (the link element that loaded the file).
// but for now, `css` is a function since the id wasn't defined in Define.urls
});
STATE_NON_CALLED :number
The value for all modules that had been queued prior to be called.
Type:
number
urls :!object.<just.Define~id, url>|!object.<just.Define~id, object.<elementAttributes>>
A list of urls that will be used (instead of ids) to load files before defining globals or non-script values.
Type:
!object.<just.Define~id, url> or !object.<just.Define~id, object.<elementAttributes>>
Examples
// js/b.js
just.Define('b', 1); // or: just.Define('js/b.js', 1);
// index.js
just.Define.urls['b'] = 'js/b.js';
just.Define('a', ['b'], function (b) {
// b === 1; > true
});
// js/index.js
just.Define('foo', 1);
just.Define('bar', 1);
// index.js
just.assign(just.Define.urls, {
'foo': 'js/index.js',
'bar': 'js/index.js'
});
just.Define('foo-bar', ['foo', 'bar'], function () {
// Will load js/index.js once.
});
just.assign(just.Define.urls, {
'id': {
'src': 'https://some.cdn.com',
'integrity': 'sha512-...',
'crossorigin': '',
'data-some-other': 'attribute'
}
});
just.Define(['id'], function () {
// Will load a <script> with the given attributes ("integrity", "crossorigin", ...).
});
just.assign(just.Define.urls, {
'id': {
'tagName': 'iframe',
'src': 'https://example.org'
}
});
just.Define(['id'], function () {
// Will load an <iframe> with the given attributes.
});
Methods
clearModule(id)
Remove some module.
Parameters:
Name | Type | Description |
---|---|---|
id | just.Define~id | The id for the module. |
configure(propertiesnon-null)
Configure any writable option in just.Define using an object.
Parameters:
Name | Type | Description |
---|---|---|
properties | object | Writable properties from just.Define. |
Example
just.Define.configure({
'urls': {}, // Same as Define.urls = {}
'handleError': function () {}, // Same as Define.handleError = function () {}
'load': 1 // Same as Define.load = 1 > throws Define.load is read-only.
})('id', [], function () {}); // Define afterwards.
findUrlsInDocument(attributeName, containeropt) → {just.Define.urls}
Finds urls within the document by selecting all the elements that contain an specific attribute and parsing that attribute as a JSON.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
attributeName | string | The attribute which defines the urls to be loaded. | |
container [optional] | Element | document |
Returns:
- Type
- just.Define.urls
Example
// Considering the following document:
< body>
< div id='a' data-urls='{"[id]": "link a.css"}'>< /div>
< script src='b.js' data-urls='{"b": "script [src]"}'>< /script>
< /body>
// then, in js:
findUrlsInDocument('data-urls');
// Should return {a: 'link a.css', b: 'script b.js'}.
handleError(exception) → {boolean}
A function to be called when an async error occur.
This:
Parameters:
Name | Type | Description |
---|---|---|
exception | Any | Some throwable exception. |
Returns:
true if you want to keep updating modules.
- Type
- boolean
load(id, onLoadnullable)
Load a module explicitly.
Parameters:
Name | Type | Description |
---|---|---|
id | url or just.Define~id | Some url or an alias defined in just.Define.urls. |
onLoad [nullable] | function | Some listener to call when the function loads. |