Templates API
tpl.pos()
asyncComputes the index of the template element, whether the passed element is the template element itself or one of its descendants, along with the data of the template containing this element, and executes the function if it was provided.
-
Accepts a DOM element as the first argument.
Accepts an optional second argument as a function. -
Returns an object with the following properties:
PropertyTypeDescriptiontplDOM elementA block containing template elements.namestringThe name of the template in which the passed element is placed.fieldDOM elementThe template element itself, even if a descendant is passed.idxnumberThe index of the template element itself, even if a descendant is passed.
Example:
await tpl.pos();
// false
await tpl.pos(null);
await tpl.pos($('body'));
// null
await tpl.pos($('[data-tpl="tplName"] .book'));
/*
{
tpl: div[data-tpl="tplName"],
name: 'tplName',
field: div.field,
idx: 0
}
*/
await tpl.pos($('[data-tpl="tplName"] > * ? 1'));
/*
{
tpl: div[data-tpl="tplName"],
name: 'tplName',
field: div.field,
idx: 1
}
*/
tpl.pos($('[data-tpl="tplName"] .book'), (e) => {
e.field.cls.add('newClass');
});