Utility functions
Table of contents
Utility Functions
All Polymer elements inherit from Polymer.Base
, which
provides a set of useful convenience functions for instances to use.
-
$$(selector)
. Returns the first node in this element’s local DOM that matchesselector
. -
toggleClass(name, bool, [node])
. Toggles the named boolean class on the host element, adding the class ifbool
is truthy and removing it ifbool
is falsey. Ifnode
is specified, sets the class onnode
instead of the host element. -
toggleAttribute(name, bool, [node])
. LiketoggleClass
, but toggles the named boolean attribute. -
attributeFollows(name, newNode, oldNode)
. Moves a boolean attribute fromoldNode
tonewNode
, unsetting the attribute (if set) onoldNode
and setting it onnewNode
. -
classFollows(name, newNode, oldNode)
. Moves a class fromoldNode
tonewNode
, removing the class (if present) onoldNode
and adding it tonewNode
. -
fire(type, [detail], [options])
. Fires a custom event. Theoptions
object can contain the following properties:-
node
. Node to fire the event on (defaults tothis
). -
bubbles
. Whether the event should bubble. Defaults totrue
. -
cancelable
. Whether the event can be canceled withpreventDefault
. Defaults tofalse
.
-
-
async(method, [wait])
. Callsmethod
asynchronously. If no wait time is specified, runs tasks with microtask timing (after the current method finishes, but before the next event from the event queue is processed). Returns a handle that can be used to cancel the task. -
cancelAsync(handle)
. Cancels the identified async task. -
debounce(jobName, callback, [wait])
. Calldebounce
to collapse multiple requests for a named task into one invocation, which is made after the wait time has elapsed with no new request. If no wait time is given, the callback is called at microtask timing (guaranteed to be before paint). -
cancelDebouncer(jobName)
. Cancels an active debouncer without calling the callback. -
flushDebouncer(jobName)
. Calls the debounced callback immediately and cancels the debouncer. -
isDebouncerActive(jobName)
. Returns true if the named debounce task is waiting to run. -
transform(transform, [node])
. Applies a CSS transform to the specified node, or host element if no node is specified.transform
is specified as a string. For example:this.transform('rotateX(90deg)', this.$.myDiv);
-
translate3d(x, y, z, [node])
. Transforms the specified node, or host element if no node is specified. For example:this.translate3d('100px', '100px', '100px');
-
importHref(href, onload, onerror)
. Dynamically imports an HTML document.this.importHref('path/to/page.html', function(e) { // e.target.import is the import document. }, function(e) { // loading error });
Note: To call
importHref
from outside a Polymer element, usePolymer.Base.importHref
. -
resolveUrl(url)
. Takes a URL relative to the<dom-module>
of an imported Polymer element, and returns a path relative to the current document. This method can be used, for example, to refer to an asset delivered alongside an HTML import.