Class: Define

just.Define(idnullable, dependencyIDs, value)

Define a value after all dependencies became available.
Source:
just.js, line 2267

Parameters:

NameTypeDescription
id
[nullable]
stringThe module id.
dependencyIDs
string[] or stringRequired module ids.
value
AnyThe 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.
Source:
just.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.
Source:
just.js, line 2479
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_CALLED :number

The value for all modules that were called.
Source:
just.js, line 2387
Type:

number

STATE_CALLING :number

The value for all modules that are being called.
Source:
just.js, line 2380
Type:

number

STATE_DEFINED :number

The initial value for all defined modules.
Source:
just.js, line 2365
Type:

number

STATE_NON_CALLED :number

The value for all modules that had been queued prior to be called.
Source:
just.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.
Source:
just.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

clear()

Empty all internal variables and writable properties.
Source:
just.js, line 2584

clearModule(id)

Remove some module.
Source:
just.js, line 2609
Parameters:
NameTypeDescription
id
just.Define~idThe id for the module.

clearModules()

Remove all modules.
Source:
just.js, line 2601

configure(propertiesnon-null)

Configure any writable option in just.Define using an object.
Source:
just.js, line 2571
Parameters:
NameTypeDescription
properties
objectWritable 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.
Source:
just.js, line 2677
Parameters:
NameTypeDefaultDescription
attributeName
stringThe attribute which defines the urls to be loaded.
container
[optional]
Elementdocument
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.
Source:
just.js, line 2618
This:

just.Define

Parameters:
NameTypeDescription
exception
AnySome throwable exception.
Returns:
true if you want to keep updating modules.
Type
boolean

init()

Finds urls within the document, adds them, and loads them.
Source:
just.js, line 2634

isDefined() → {boolean}

Check if a module is defined.
Source:
just.js, line 2548
Returns:
Type
boolean

load(id, onLoadnullable)

Load a module explicitly.
Source:
just.js, line 2556
Parameters:
NameTypeDescription
id
url or just.Define~idSome url or an alias defined in just.Define.urls.
onLoad
[nullable]
functionSome listener to call when the function loads.

load()

Same as Define.load, but chainable.
Source:
just.js, line 2704