Table of Contents

$p.dyn (dynamic loader)

This module allows to inject dynamically loaded content to the web page.

The mechanism it uses is that of embedded JSON snippets in the HTML source code.

An example for such a snippet could be:

<script type="application/json"> {
  "action": "test-action",
  "parameter": "some value"
} </script>

The JSON snippet should contain a single object with a property called action that defines which loader action should be applied. All other properties are dependent on the action handler that is assigned to the specific action ID.

Hint: for an example, see the homepage of this site, which uses a custom content loader to insert the documentation widget.

Methods

Publicly available methods are:

$p.dyn.parse(scope)

This method is normally called in the module’s init() method with a scope of the entire Document.

It is however possible to re-parse a section of the page, e.g. after it had been altered, to make sure embedded JSON snippets are handled.

Note that there is no mechanism that prevents re-parsing of already handled JSON snippets. If not handled carefully, this can lead to infinite loops and subsequent browser crashes.

$p.dyn.action.register(id, callback)

This is used to register an action handler in the module. This should happen relatively early in the load process to make sure that it is available at the time when the page is parsed for snippets.

Parameters:

  1. id – the action id, as it is used in the action property in the JSON snippet (example: “test-action”)
  2. callback – a function reference that will be called when a JSON snippet with the named action is found.

Note: The callback function will be called with two parameters. These are:

  1. An HtmlElement reference to the parent object of the <script> element,
  2. The parsed JSON as JavaScript Object.

See also

FIXME