Created
May 7, 2011 07:52
-
-
Save jmoyers/960300 to your computer and use it in GitHub Desktop.
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
if (module) { | |
var inherits = require('util').inherits, | |
EventEmitter = require('events').EventEmitter, | |
_ = require('underscore'); | |
} | |
function Model(attribs){ | |
EventEmitter.apply(this, arguments); | |
this._attributes = attribs || {}; | |
this.track(Object.keys(this._attributes)); | |
} | |
inherits(Model, EventEmitter); | |
Model.prototype.track = function(keys){ | |
keys = _.isArray(keys) | |
? keys | |
: [keys]; | |
var me = this; | |
_.each(keys, function(key){ | |
me.__defineGetter__(key, function(){ | |
return me.get(key); | |
}) | |
me.__defineSetter__(key, function(val){ | |
return me.set(key, val); | |
}); | |
}); | |
return me; | |
} | |
Model.prototype.get = function(key, val){ | |
return this._attributes[key]; | |
} | |
Model.prototype.set = function(key, val){ | |
this._attributes[key] = val; | |
this.emit('change', {}[key] = val); | |
} | |
if (module) { | |
module.exports = Model; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment