Define a value after all dependencies became available.
A few things to consider: This is not intended to be AMD compliant. This does not check file contents, so it won't check if the file defines an specific id. Urls passed as dependencies are considered ids, so they must be defined in just.Define.urls in order to be loaded first. require , module and exports are not present in this loader, but you can emulate them.Recursive and circular dependencies pass a recursive module as argument within another recursive module (instead of the returned value). Please, avoid using them or use them carefully. Source: core.js , line 2267 Parameters: Name Type Description id [nullable] string The module id. dependencyIDs string[] or string Required module ids. value Any The module value.
Example Example 1. // 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.
Notes: Deprecated since 1.0.0-rc.24. It raises a security error over a CDN. If the value for the global is a string, the property will be accessed from window. I.e.: 'some.property' will access to window.some.property .If a module is defined with the same id, the module will take precedence. If a non-script is defined with the same id, a non-script value will take precedence. Source: core.js , line 2538 Type: object.<just.Define~id, Any>
Examples Example 1. // index.js
just.Define.globals['just'] = 1;
just.Define('index', ['just'], function (just) {
// just === 1; > true
});
Example 2: Defining a global on file load.// 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};
});
Example 3: Defining a global after a file has loaded already.// 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.
Note: If a module is defined with the same id, the module will take precedence.
Source: core.js , line 2479 Type: object.<just.Define~id, Any>
Example Example 1. 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
});
The value for all modules that are being called.
Source: core.js , line 2380 Type: number
The value for all modules that had been queued prior to be called.
Source: core.js , line 2373 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.
Note: If you need to load files when you require some id, you need to specify those urls here. If you do so, you must Define that id/url within that file.
Starting from v1.0.0-rc.23, you can pass an object to specify the attributes for the loaded element.
Source: core.js , line 2454 Type: !object.<just.Define~id, url > or !object.<just.Define~id, object.<elementAttributes>>
Examples Example 1. // 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
});
Example 2: Using multiple ids with the same url// 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.
});
Example 3: Since v1.0.0-rc.23: Adding custom attributes to the loaded element.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", ...).
});
Example 4: Since v1.0.0-rc.23: Load a custom element.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 Remove some module.
Source: core.js , line 2609 Parameters: Name Type Description id just.Define~id The id for the module.
Finds
urls within the document by selecting all the elements that contain an specific attribute and parsing that attribute as a JSON.
Note Values within brackets will be replaced with actual attributes for that element.
I.e.: <span a='123' data-urls='{"[a]456": "[a]456"}'></span> will become: {123456: '123456'}
Source: core.js , line 2677 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 Example 1. // 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.
Source: core.js , line 2618 This: just.Define
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.
Source: core.js , line 2556 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.