Skip to content

Instantly share code, notes, and snippets.

@blahutka
Last active August 29, 2015 14:21
Show Gist options
  • Save blahutka/e648148f374318098938 to your computer and use it in GitHub Desktop.
Save blahutka/e648148f374318098938 to your computer and use it in GitHub Desktop.
Riot model
// Source: https://github.com/muut/riotjs/issues/727
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
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment