Skip to content

Instantly share code, notes, and snippets.

@lusentis
Created August 16, 2013 17:42
Show Gist options
  • Save lusentis/6251903 to your computer and use it in GitHub Desktop.
Save lusentis/6251903 to your computer and use it in GitHub Desktop.
Evented Objects for the browser
/*jshint browser:true, laxcomma:true, indent: 2, eqnull:true, devel:true */
/*global define */
define('eo', ['vendor/asEvented'], function (asEvented) {
'use strict';
var Eo = function (props, name) {
if ('string' === typeof props) {
this._fromJSON(props);
} else {
this._fromObject(props);
}
if (this.prop('name') === undefined) {
this.prop('_name', name || 'Eo instance');
}
this.trigger('create');
};
asEvented.call(Eo);
Eo.prototype.prop = function (name, val) {
if (val === undefined) {
return this._props[name];
} else {
this._props[name] = val;
this.trigger('change:' + name, [name, val]);
this.trigger('change', [name, val]);
}
};
Eo.prototype.toJSON = function () {
JSON.stringify(this._props);
};
Eo.prototype._fromJSON = function (json) {
this._props = JSON.parse(json);
};
Eo.prototype._fromObject = function (obj) {
this._props = obj;
};
Eo.createFromArray = function (arr) {
var ret = [];
arr.forEach(function (item) {
ret.push(new Eo(item));
});
return ret;
};
return {
Eo: Eo
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment