Edit this page

up.script up.macro(selector, options, macro)
JavaScript function

Registers a compiler that is run before all other compilers.

A macro lets you set attributes that will be compiled afterwards.

If you want default attributes for every link and form, consider customizing your navigation options or configure Unpoly to handle everything.

Example

You will sometimes find yourself setting the same combination of UJS attributes again and again:

<a href="/page1" up-layer="new modal" up-class="warning" up-animation="shake">Page 1</a>
<a href="/page1" up-layer="new modal" up-class="warning" up-animation="shake">Page 2</a>
<a href="/page1" up-layer="new modal" up-class="warning" up-animation="shake">Page 3</a>

We would much rather define a new [shake-modal] attribute that let's us write the same links like this:

<a href="/page1" shake-modal>Page 1</a>
<a href="/page2" shake-modal>Page 2</a>
<a href="/page3" shake-modal>Page 3</a>

We can define the [shake-midal] attribute by registering a macro that sets the [up-layer], [up-class] and [up-animation] attributes for us:

up.macro('[shake-modal]', function(link) {
  link.setAttribute('up-layer', 'new modal')
  link.setAttribute('up-class', 'warning')
  link.setAttribute('up-animation', 'shake')
})

Parameters

selector
required

The selector to match.

string
options
required

See options for up.compiler().

macro
required

The function to call when an element matching selector is inserted.

Up to three arguments can be accepted:

  1. The newly inserted element matching the selector
  2. Any data attached to the element
  3. Meta information about the current render pass

The function may return a destructor function that is called before it is removed from the DOM. The destructor function is called with the compiled element.

The compiler function can be async.

Function(element, data, meta): (Function|Array<Function>|Promise<Function>|undefined)