RIOT.JS Tutorial
Selected Reading
- RIOT.JS - Discussion
- RIOT.JS - Useful Resources
- RIOT.JS - Quick Guide
- RIOT.JS - Observables
- RIOT.JS - Mixin
- RIOT.JS - Loops
- RIOT.JS - Accessing DOM
- RIOT.JS - Event Handling
- RIOT.JS - Yield
- RIOT.JS - Conditional
- RIOT.JS - Styling
- RIOT.JS - Expressions
- RIOT.JS - Tags
- RIOT.JS - First Application
- RIOT.JS - Environment Setup
- RIOT.JS - Overview
- RIOT.JS - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
RIOT.JS - Observables
RIOT.JS - Observables
Observables mechanism allows RIOT to send events from one tag to another. Following APIs are important to understand RIOT observables.
riot.observable(element) − Adds Observer support for the given object element or if the argument is empty a new observable instance is created and returned. After this the object is able to trigger and psten to events.
var EventBus = function(){ riot.observable(this); }
element.trigger(events) − Execute all callback functions that psten to the given event.
sendMessage() { riot.eventBus.trigger( message , Custom 10 Button Cpcked! ); }
element.on(events, callback) − Listen to the given event and execute the callback each time an event is triggered.
riot.eventBus.on( message , function(input) { console.log(input); });
Example
Following is the complete example.
custom10Tag.tag
<custom10Tag> <button oncpck = {sendMessage}>Custom 10</button> <script> sendMessage() { riot.eventBus.trigger( message , Custom 10 Button Cpcked! ); } </script> </custom10Tag>
custom11Tag.tag
<custom11Tag> <script> riot.eventBus.on( message , function(input) { console.log(input); }); </script> </custom11Tag>
custom9.htm
<html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/pbs/riot/3.13.2/riot+compiler.min.js"></script> </head> <body> <custom10Tag></custom10Tag> <custom11Tag></custom11Tag> <script src = "custom10Tag.tag" type = "riot/tag"></script> <script src = "custom11Tag.tag" type = "riot/tag"></script> <script> var EventBus = function(){ riot.observable(this); } riot.eventBus = new EventBus(); riot.mount("*"); </script> </body> </html>
This will produce following result −
Advertisements