How to check if a riot tag exists?

482 Views Asked by At

How can I check if a riot tag has already been loaded and compiled (in-browser with script tag), in order to avoid doing it again, programmatically.

In other words, what should I use instead of doesTagExist function in my simplified code, below?

if (!doesTagExist('my-tag')) {
    riot.compile('/path/to/my-tag', function() {
        riot.mount('dom-node', 'my-tag');
    });
} else {
    riot.mount('dom-node', 'my-tag');
}
1

There are 1 best solutions below

0
On

had same problem. After bit of research I think you can't get it directly. Implementation is stored inside __TAG_IMPL which is not accessible from outside. You can however access selector for all implemented tags via riot.util.tags.selectTags(), which returns comma separated list of selectors i.e. datepicker,[data-is="datepicker"].

Oneliner for convenience

riot.util.tags.selectTags().search(/(^|,)my-tag($|,)/g) >= 0

or depending on your purity inclination

riot.util.tags.selectTags().search('"my-tag"')

Note, that first version is future-proof, if riot decides to start using single commas in selector.