Last active
August 29, 2015 14:21
-
-
Save blahutka/e648148f374318098938 to your computer and use it in GitHub Desktop.
Riot model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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