Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 960 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Riot JS - Tag Options

2:20 JavaScript lesson by

Tag Options allow you to abstract your custom tag elements into reusable objects by allowing the tag-user to specify the dynamic parameters of their component.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Tag Options allow you to abstract your custom tag elements into reusable objects by allowing the tag-user to specify the dynamic parameters of their component.

What if I wanted the greeting part of my "Hello World" tag to be customizable? If I wanted to be able to do like Angular does, and pass in some parameters? Let's replace "Hello" with a data-bound property of ops.greeting. Custom elements can accept parameters called Options.

This ops object is special in Riot. It will contain all the options passed into a tag. There are two ways to pass in options. If you're working in an uncompiled file, like the body of the index file, then you pass parameters in via the Mount Call.

Pass an object as the second parameter whose keys and values are option names and values. You can send along extra data if you'd like, but if you don't use it in your tag, it's ignored. We'll refresh and see if our options are working properly...Greeting worked and punctuation was ignored.

To demonstrate the second way, I have to make another tag. Let's make a trim tag. I just want a simple way to trim a string value. With data binding, I'll pass in a value that I want to trim...and I'll pass in a length for the trim.

Everything in the curly brackets is regular JavaScript. You can go crazy in there, but really you should keep it simple. Here's a "Gotcha." It's important to use a nested HTML element like span because otherwise, Riot things are just trying to write some badly formatted JavaScript.

The second way to pass in options. If you're working in a complied file like this .tag file, then you use custom tags without any call to riot.mount. The tags are mounted automatically.

You pass in options as attributes. We'll set our value option and our length to three. We'll refresh and see if our trim tag options are working.

Of course, you can also pass data-bound properties to your custom tag options and they'll work just as expected. Now, it's trimming the local this.what value.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?