Created
August 16, 2013 17:42
-
-
Save lusentis/6251903 to your computer and use it in GitHub Desktop.
Evented Objects for the browser
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
/*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