RIOT.JS - Gestione degli eventi

Possiamo allegare un evento agli elementi HTML nello stesso modo in cui accediamo agli elementi HTML utilizzando l'oggetto refs. Come primo passaggio aggiungiamo un attributo ref a un elemento DOM e vi accediamo utilizzando this.ref nel blocco di script del tag.

  • Attach ref - Aggiungi l'attributo ref a un elemento DOM.

<button ref = "clickButton">Click Me!</button>
  • Use the refs object- Ora usa l'oggetto refs nell'evento mount. Questo evento viene generato quando RIOT monta il tag personalizzato e popola l'oggetto refs.

this.on("mount", function() {
   console.log("Mounting");
   console.log(this.refs.username.value);
})

Esempio

Di seguito è riportato l'esempio completo.

custom5Tag.tag

<custom5Tag>
   <form>
      <input ref = "username" type = "text" value = "Mahesh"/>
      <input type = "submit" value = "Click Me!" />
   </form>
   <script>
      this.on("mount", function() {
         console.log("Mounting");
         console.log(this.refs.username.value); 
      })
   </script>
</custom5Tag>

custom5.htm

<html>
   <head>
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/riot/3.13.2/riot+compiler.min.js"></script>
   </head>
   <body>
      <custom5Tag></custom5Tag>
      <script src = "custom5Tag.tag" type = "riot/tag"></script>
      <script>
         riot.mount("custom5Tag");
      </script>
   </body>
</html>

Questo produrrà il seguente risultato: