Foundry Industrial Strength JavaScript

Creating New Modules On The Fly

Not every module will exist in the HTML file at page load. Sometimes you need to create new modules as a result of a user clicking on a button or link. Foundry gives you an easy way to do this.

Download The Demo

View The Demo: New Modules On The Fly

What You'll Need For This Tutorial

  1. The Foundry Starter Project
  2. A basic understanding of how Modules work in Foundry

Basics Of Dynamically Creating New Modules

Foundry comes with functionality to create and append new modules to the document at runtime when the user clicks on a button or link. It allows you to render a client side template to get the initial HTML for the new modules, as well as giving you all the configuration options available elsewhere in Foundry.

HTML For Creating New Modules

<button
    data-actions="newModules.createModule"
    data-action-params='{
        "newModules.createModule": {
            "module": {
                "type": "FooModule",
                "options": {
                    "foo": "bar"
                },
                "template": "foo"
            },
            "container": {
                "selector": ".main",
                "renderData": {
                    "title": "Foo"
                }
            }
        }
    }'
>New Module</button>

There are two main pieces:

  1. The data-actions attribute must have a value of newModules.createModule — this is baked in to Foundry.
  2. The data-action-params attribute is required, and provides information to construct the new module.

The data-action-params attribute must be in this format:

Structure of data-action-params

{
    "newModules.createModule": {
        "module": {
            "type": "FooModule", // What would normally go in data-modules attribute
            "options": {
                // What would normally go in data-module-options attribute
            },
            "template": "foo" // <script type="text/html" data-template="foo" />
        },
        "element": {
            "tag": "DIV" // Tag name of module's root element
        },
        "container": {
            "selector": ".foo", // Element that should contain the new module
            "insert": "bottom|top", // Where to insert new module in container
            "renderData": {
                // Arbitrary data used to render the new module
            }
        }
    }
}

Rendering the Client Side Template

The client side template is just a special <script> tag on the page. It uses a very simple template language with tags like #{foo}, where foo is a key in the renderData. Foundry gives you several pieces of data by default:

<script type="text/html" data-template="foo">
    Date: #{createdAt}<br>
    GUID: #{guid}<br>
    Timestamp: #{timestamp}<br>
    <button data-actions="#{controllerId}.addItem">Add Item</button>
</script>

The renderData in the data-action-params extends the data used to render the client side template.

A Quick Recap

Foundry automates the process of creating new modules at runtime. Any element on the page with data-actions="newModules.createModule" will trigger this behavior. The data-action-params attribute contains metadata about the new module, including the module type, runtime configurable options, the root root element that Foundry should create, and the location on the page where the new module gets inserted.

What's Next?

This is the last tutorial for Foundry. More are in the works, but for now download Foundry and start playing around. You can always view all tutorials, or browse the code. Check out the Foundry Blog for news, updates and how-to guides.