by
36
8
4,464
9
Top 1% !
Popular
Famous
Nice
Useful
Easy-to-find
Specified
MultiPlatform
Popularity: 1949th place
Languagejavascript
LicenseMIT_X11
TechnologyMVC

Related

Make anything a SignlSEE broadcasting transmitter

create observables for pubsub, or home-spun MVcwWCvvV666. The kids will eat their vegies and grandma finaly went to bed! Its a great conversation starter!
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/362432/Make-anything-a-SignlSEE-broadcasting-tr?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// OBSERVE ME!!!! var SeeMe=(function(obj) { var watchd={},mod1=obj||{}; function Model (mod1) { this.base = mod1; this.get = function(k) { if (k) { var u = this.base; return u[k]; } else { var uu = this.base; return uu; } }; } watchd = Model; watchd.prototype = Signals; watchd.prototype.on = function(v, cbk) { this.receive(v, function(d) { cbk.call(this.base, d); }); }; watchd.prototype.emit = function(v) { this.signal("change", v); }; watchd.prototype.off = function(v) { this.drop(v); }; watchd.prototype.set = function(k, v) { if (v && v !== this.base[k]) { if (v==Array||v!=Array&&k!=Array) { this.base[k]=v; } else if(v==Array) { this.base[k].push(v); } this.signal("change", this.base); } }; watchd = new watchd(mod1); return watchd; }(Signals)); var user = { name1: "Richard", name2: "Longwood", occupation: "Software Engineer", age: 40, title: 'User!', subTitle: "Edit Me!", canEdit: true, skills: ["ajax", "css", "javascript", "HTML5"] }; // Create an instance of our new type.... SeeMe.base = user; var User = Object.create(SeeMe); // Now we'll see that the values are set and accessable User.get(); // Object {name1: "Richard", name2: "Longwood", occupation: "Software Engineer", age: 40, title: "User!"…} User.get("skills"); // ["ajax", "css", "javascript", "HTML5"] User.set("skills","blah"); User.get("skills"); // "blah" User.set("skills",["ajax", "css", "javascript", "HTML5"]); User.get("skills"); // ["ajax", "css", "javascript", "HTML5"] User.get("skills")[2]; // "javascript" User.set("skills",[1,2,3]); User.get("skills"); // [1, 2, 3] /* Now the kewl part... lets wire up a signal channel to fire an alert whenever our User is changed... */ User.on("change", function(d) { alert(JSON.stringify(d)); }); // Now we'll reassign them and watch our alerts go BOING! User.set("age", "OlD"); User.set("name1", "HEaTH"); User.set("name2", "DERN!"); User.set("title", "who kares"); User.set("subTitle", "whatever"); User.set("occupation", "coding"); User.set("skills", [1,2,3,4,5,6,7,8,9,0]); // Just to make sure the new values were pesisted... User.get(); User.get("age"); User.get("name1"); User.get("name2"); User.get("title"); User.get("subTitle"); User.get("occupation"); User.get("skills"); /* The modules arent in the correct order of dependancy, but I wanted the newest part to be featured. Below is my Signals eventing system. The module at the top, SEEKr.... is a signaling, (observable), wrapper that can make almost anything a signal transmitter for use in pub-sub, mediator, whatever you like. theres also a small example just below the SEEKr module. */ var Signals = (function() { 'use strict'; var sigCache = {}, singleRtnVal = false, received = function(eventName, func) { return wen(eventName, function(evnt) { return func(evnt); }); }, dropReceivers = function(eventName, func) { if (eventName && func && typeof func === "function") { dropReceiver(eventName, func); return this; } if (!eventName) { sigCache = {}; return this; } if (sigCache && sigCache[eventName]) { sigCache[eventName] = null; } return this; }, setSingleRV = function(val) { singleRtnVal = val; return this; }, getSingleRV = function() { if (receivers.hasOwnProperty('singleRtnVal')) { return singleRtnVal; } else { return true; } }, dropReceiver = function(eventName, func) { if (sigCache && sigCache[eventName]) { var sigLst = sigCache[eventName]; if (isAry(sigLst)) { var idx = -1; for (var i = 0, l = sigLst.length; i < l; i++) { if (sigLst[i] === func || (sigLst[i].receiver && sigLst[i].receiver === func)) { idx = i; break; } } if (idx < 0) { return this; } sigLst.splice(idx, 1); if (!sigLst.length) { delete sigCache[eventName]; } } else if (sigLst === func || (sigLst.receiver && sigLst.receiver === func)) { delete sigCache[eventName]; } } return this; }, signalOnce = function(eventName, func) { setSingleRV(true); var slf = this, onit = function() { dropReceiver(eventName, onit); func.apply(slf, arguments); }; onit.receiver = func; wen(eventName, onit); return this; }, castSignal = function(eventName, args) { var recvrLst = receiverObjects(eventName), k = {}, recvr = [], i = 0, rspns = {}; for (k in recvrLst) { if (recvrLst.hasOwnProperty(k)) { i = recvrLst[k].length; while (i--) { recvr = recvrLst[k][i]; if (recvr.signalOnce === true) { dropReceiver(eventName, recvr); } rspns = recvr.call(this, args || arguments || []); if (rspns === getSingleRV()) { dropReceiver(eventName, recvr); } } } } return this; }; function isAry(obj) { return (obj.constructor === Array); } function receiverObjects(eventName) { var recvrLst = receivers(eventName), rspns; if (isAry(recvrLst)) { rspns = {}; rspns[eventName] = recvrLst; } return rspns || recvrLst; } function receivers(eventName) { if (!sigCache) { sigCache = {}; } if (!sigCache[eventName]) { sigCache[eventName] = []; } if (!isAry(sigCache[eventName])) { sigCache[eventName] = [sigCache[eventName]]; } return sigCache[eventName]; } function wen(eventName, func) { if (!sigCache) { sigCache = {}; } if (!sigCache[eventName]) { sigCache[eventName] = func; } else if (isAry(sigCache[eventName])) { sigCache[eventName].push(func); } else { sigCache[eventName] = [sigCache[eventName], func]; } return this; } return { signal: castSignal, receive: received, once: signalOnce, channels: receivers, drop: dropReceivers }; }());
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus