Global methods

Anymod is a global JavaScript object that provides access to all of the mods on a page along with several useful utility functions.

Anymod.ready

Anymod.ready(callback)

Executes a callback once the Anymod script has finished loading.

Usage

Anymod.ready(function () {
  console.log('Hello world!')
})
Anymod.ready(function () {
  console.log('Hello again!')
})
1
2
3
4
5
6

Result

Runs the callbacks when Anymod has finished loading.

Hello world!
Hello again!
1
2

TIP

You don't need to use Anymod.ready() in a mod's own code: it will already be ready.

Anymod.render

Anymod.render(callback) -> [promise]

Renders mod tags that were added to the page after Anymod's initial run. Can be used with a callback or a promise.

Usage

// With a callback
Anymod.render(function() {
  console.log('Render complete')
})
1
2
3
4
// With a promise
Anymod.render()
.then(function() {
  console.log('Render complete')
})
1
2
3
4
5

Result

Mod tags that were asynchronously added to the page are rendered.

<!-- Hello world text -->
<div id="anymod-abcde"></div>
1
2

Mod is rendered:

<!-- Hello world text -->
<div id="anymod-abcde" abcde data-anymod="abcde-0000000" data-anymod-mounted>
  Hello, World!
</div>
1
2
3
4

Behavior

Mods are rendered automatically when the Anymod script loads. However, you may not have all mods on the page at that time, so you can call Anymod.render() at any time to render mods that have been added since initial page load. There are a few scenarios where this may happen, and the behavior for each is shown below:

Scenario Behavior
No unrendered mods Execute promise or callback, if any.
New, unrendered mods Fetch data with single API call for all new mods, then render unrendered mods, then execute promise or callback, if any.
Previously fetched (but now unrendered) mods Use the existing data from Anymod.Page to render unrendered mods, then execute promise or callback, if any.

TIP

Anymod.render() is useful when using mods within modern JavaScript frameworks. See the Anymod guides for React, Vue.js, or Angular for more specifics.

Anymod( key )

Anymod(key)

Returns the data object for a given mod.

Usage

Anymod('abcde')
1

Result

{
  key: "abcde", 
  data: {
    message: "Hello, world!"
    items: []
  }, 
  html: "<div>Hello, world!</div>",
  css: "[abcde]{background:red;padding:10px...}",
  js: "console.log('hello');",
  cssAssets: ["https://cdn.example.com/stylesheet.css"],
  jsAssets: []
}
1
2
3
4
5
6
7
8
9
10
11
12

Similarly, you can read a specific piece of data:

Anymod('abcde').data.message
// --> "Hello, world!"
1
2

Anymod.Project

You can get the current project at any time with Anymod.Project

Anymod.Project
// -> "8PP6M2"
1
2

Anymod.ApiUrl

You can get the Anymod API URL with Anymod.ApiUrl

Anymod.ApiUrl
// -> "https://api.anymod.com/v2/"
1
2

Anymod.Event

Anymod comes with a built-in message bus for communication between mods or within your application. It is based on Eev and has three methods:

  • Anymod.Event.on(eventName, callback)

    Listen for a custom event, which can be triggered by Anymod.Event.emit. The callback will receive whatever data is emitted.

  • Anymod.Event.off(eventName, callback)

    Remove custom event listener(s). eventName and callback must match the event as defined in Anymod.Event.on.

  • Anymod.Event.emit(eventName, data)

    Trigger an event. Any additional arguments will be passed into the callback function for Anymod.Event.on.

Usage

function logHello (data) {
  console.log('Hello, ' + data.name + '!')
}

// Create event listener
Anymod.Event.on('custom:message', logHello)

// Trigger event
Anymod.Event.emit('custom:message', { name: 'everyone' })
// -> "Hello, everyone!"

// Remove event listener
Anymod.Event.off('custom:message', logHello)

// Trigger event again
Anymod.Event.emit('custom:message', { name: 'everyone' })
// -> Does nothing (event listener was removed with .off())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Example (on)

The following mod has an event listener added:

Anymod.Event.on('updateMod', function(data) {
  title.innerHTML = data.title
  text.innerHTML = data.text
})
1
2
3
4

Open your browser console and run the following to update the mod:

Anymod.Event.emit('updateMod', { title: 'Hello, world', text: 'New text' })
1

Example (emit)

The following mod has an event emitter added:

Anymod.Event.emit('updateMod', {
  title: titleInput.value,
  text: textInput.value
})
1
2
3
4

Because it emits the updateMod event, this mod can communicate with the first mod that has the event listener added.

Click the Send event button to update the first mod's text.

TIP

View the code for the listener and emitter mods to learn more.

Anymod.buildImage

Anymod.buildImage(image, { options })

Resizes and crops an image based on options inputs.

Usage

// Inside of Anymod editor JavaScript panel
Anymod.buildImage(mod.data.image, { w: 300, h: 200, c: 'fill', g: 'face' })
1
2
<!-- Handlebars: inside of Anymod editor HTML panel -->
<img src="{{ buildImage image w=300 h=200 c='fill' g='face' }}" />
1
2

Image transformations are based on Cloudinary's interface. See the following table for more information:

Option Type Default Description
w Integer - Width of the image in pixels
h Integer - Height of the image in pixels
c String fit Cropping mode: scale, fit, mfit, fill, lfill, limit, pad, lpad, mpad, crop, or thumb.
g String center Cropping gravity: see link for options.

Example

Given an image field with an uploaded image:

You can resize and crop the image in your JavaScript (JS panel) or with Handlebars (HTML panel):

// JavaScript (JS panel)
var newImage = Anymod.buildImage(mod.data.image, { w: 400, h: 150, c: 'fill' })
1
2
<!-- Handlebars (HTML panel) -->
<img src="{{ buildImage image w=400 h=150 c='fill' }}" />
1
2

If the image has a face, you can smart crop by using the g: 'face' option:

// JavaScript (JS panel)
var newImage = Anymod.buildImage(mod.data.image, { w: 150, h: 150, c: 'crop', g: 'face' })
1
2
<!-- Handlebars (HTML panel) -->
<img src="{{ buildImage image w=150 h=150 c='crop' g='face' }}" />
1
2

TIP

See the image manipulation example for more.

Anymod.loadScript

Anymod.loadScript(url, callback, { options })

Loads a script tag into the <body> element as denoted by url. Executes an optional callback function once the script is loaded.

Usage

Anymod.loadScript('https://www.google.com/recaptcha/api.js', function () {
  console.log('reCaptcha script has loaded.')
}, { defer: true })
1
2
3

Options

Property Type Default Description
id string - Sets the id attribute for the <script> tag.
async boolean true Sets the async attribute for the <script> tag.
defer boolean false Sets the defer attribute for the <script> tag.

TIP

You can add scripts directly to a mod without invoking loadScript. See custom scripts for more.

Anymod.loadStylesheet

Anymod.loadStylesheet(url, callback, { options })

Loads a stylesheet tag into the <head> element as denoted by url. Executes an optional callback function once the stylesheet is loaded.

Usage

Anymod.loadStylesheet('https://cdnjs.cloudflare.com/ajax/libs/bulma/0.4.2/css/bulma.css', function () {
  console.log('Bulma CSS has loaded.')
}, { id: 'my-stylesheet' })
1
2
3

Options

Property Type Default Description
id string - Sets the id attribute for the <link> tag.

TIP

You can add stylesheets directly to a mod without invoking loadStylesheet. See custom stylesheets for more.

Last Updated: 10/5/2018, 10:07:37 PM