Created
December 24, 2010 05:05
-
-
Save unscriptable/753916 to your computer and use it in GitHub Desktop.
cujo.Stateful
This file contains hidden or 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
| /* | |
| cujo.Stateful | |
| (c) copyright 2010, unscriptable.com | |
| author: john | |
| LICENSE: see the LICENSE.txt file. If file is missing, this file is subject to the AFL 3.0 | |
| license at the following url: http://www.opensource.org/licenses/afl-3.0.php. | |
| Adds dojo.Stateful behavior to objects without the dojo.declare turds and overhead | |
| Use cujo._Stateful as a mixin in a multiple-inheritance pattern. Example: | |
| dojo.declare('myClass', cujo._Stateful, { ... }); // mixin | |
| */ | |
| define(/* cujo/Stateful */ ['cujo'], function(cujo) { | |
| var op = Object.prototype; | |
| function set (obj, cb, name, value) { | |
| var old; | |
| function _set (prop, val) { | |
| var old = obj[prop]; | |
| obj[prop] = val; | |
| for (var i = 0, len = cb && cb.length; i < len; i++) { | |
| try { | |
| cb[i](prop, old, val); | |
| } | |
| catch (ex) { | |
| console && console.log && console.log(ex); | |
| } | |
| } | |
| } | |
| if (typeof name === 'object') { | |
| for (var p in name) { | |
| if (!(p in op)) { | |
| _set(p, name[p]); | |
| } | |
| } | |
| } | |
| else { | |
| _set(name, value); | |
| } | |
| return obj; | |
| } | |
| function P () {} | |
| cujo.Stateful = function (obj, options) { | |
| var callbacks = {}, | |
| proto = { | |
| get: function (name) { | |
| return this[name]; | |
| }, | |
| set: function (name, value) { | |
| return set(this, callbacks, name, value); | |
| }, | |
| watch: function (name, callback) { | |
| var cb = callbacks[name]; | |
| if (!cb) { | |
| cb = callbacks[name] = []; | |
| } | |
| // for dojo.Stateful compatibility, we need to set the context to "this" | |
| var self = this; | |
| cb.push(function () { callback.apply(self, arguments); }); | |
| return { | |
| unwatch: function () { | |
| var pos = cb.length; | |
| while (--pos >= 0) { | |
| if (cb[pos] == callback) { | |
| cb.splice(pos, 1); | |
| break; | |
| } | |
| } | |
| } | |
| }; | |
| } | |
| }; | |
| P.prototype = proto; | |
| var stfuObj = new P(); | |
| for (var p in obj) { | |
| if (!(p in op)) { | |
| stfuObj[p] = obj[p]; | |
| } | |
| } | |
| return stfuObj; | |
| }; | |
| return cujo.Stateful; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment