JSFiddle - React, Tailwind, and code Playground

by gianlucaguarini

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/riot/2.0.15/riot+compiler.js"></script>

JavaScript

var Model = function (attrs) {
  // init
  this.attributes = attrs || {}
  riot.observable(this)
    
  // set method
  this.set = function (key, val, options) {

    options = options || {}
    this.attributes[key] = val

    if (options.silent !== true)
      this.trigger(key + '_changed', val)
    
    return this
  }
  
  // get method
  this.get = function (key) {
    return this.attrbutes[key]
  }

  return this

}

var person = new Model({
  name: 'john'
})

person.on('name_changed', function (name) {
  console.log('the new name is "%s"', name)
})

person.set('name', 'jack')

// here the name is changed but no event gets triggered
person.set('name', 'noemi', {
  silent: true
})