Created
August 27, 2014 16:42
-
-
Save wmakeev/9235fa2bba2e12d054bc to your computer and use it in GitHub Desktop.
Mediator
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
var mediator = (function(){ | |
var subscribe = function(channel, fn){ | |
if (!mediator.channels[channel]) mediator.channels[channel] = []; | |
mediator.channels[channel].push({ context: this, callback: fn }); | |
return this; | |
}, | |
publish = function(channel){ | |
if (!mediator.channels[channel]) return false; | |
var args = Array.prototype.slice.call(arguments, 1); | |
for (var i = 0, l = mediator.channels[channel].length; i < l; i++) { | |
var subscription = mediator.channels[channel][i]; | |
subscription.callback.apply(subscription.context, args); | |
} | |
return this; | |
}; | |
return { | |
channels: {}, | |
publish: publish, | |
subscribe: subscribe, | |
installTo: function(obj){ | |
obj.subscribe = subscribe; | |
obj.publish = publish; | |
} | |
}; | |
}()); | |
//Pub/sub on a centralized mediator | |
mediator.name = "tim"; | |
mediator.subscribe('nameChange', function(arg){ | |
console.log(this.name); | |
this.name = arg; | |
console.log(this.name); | |
}); | |
mediator.publish('nameChange', 'david'); //tim, david | |
//Pub/sub via third party mediator | |
var obj = { name: 'sam' }; | |
mediator.installTo(obj); | |
obj.subscribe('nameChange', function(arg){ | |
console.log(this.name); | |
this.name = arg; | |
console.log(this.name); | |
}); | |
obj.publish('nameChange', 'john'); //sam, john |
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
Mediator = function() { | |
var debug = function() { | |
// console.log or air.trace as desired | |
}; | |
var components = {}; | |
var broadcast = function(event, args, source) { | |
if (!event) { | |
return; | |
} | |
args = args || []; | |
//debug(["Mediator received", event, args].join(' ')); | |
for (var c in components) { | |
if (typeof components[c]["on" + event] == "function") { | |
try { | |
//debug("Mediator calling " + event + " on " + c); | |
source = source || components[c]; | |
components[c]["on" + event].apply(source, args); | |
} catch (err) { | |
debug(["Mediator error.", event, args, source, err].join(' ')); | |
} | |
} | |
} | |
}; | |
var addComponent = function(name, component, replaceDuplicate) { | |
if (name in components) { | |
if (replaceDuplicate) { | |
removeComponent(name); | |
} else { | |
throw new Error('Mediator name conflict: ' + name); | |
} | |
} | |
components[name] = component; | |
}; | |
var removeComponent = function(name) { | |
if (name in components) { | |
delete components[name]; | |
} | |
}; | |
var getComponent = function(name) { | |
return components[name]; // undefined if component has not been added | |
}; | |
var contains = function(name) { | |
return (name in components); | |
}; | |
return { | |
name : "Mediator", | |
broadcast : broadcast, | |
add : addComponent, | |
rem : removeComponent, | |
get : getComponent, | |
has : contains | |
}; | |
}(); | |
// Using | |
Mediator.add('TestObject', function() { | |
var someNumber = 0; // sample variable | |
var someString = 'another sample variable'; | |
return { | |
onInitialize: function() { | |
// this.name is automatically assigned by the Mediator | |
alert(this.name + " initialized."); | |
}, | |
onFakeEvent: function() { | |
someNumber++; | |
alert("Handled " + someNumber + " times!"); | |
}, | |
onSetString: function(str) { | |
someString = str; | |
alert('Assigned ' + someString); | |
} | |
} | |
}()); | |
Mediator.broadcast("Initialize"); // alerts "TestObject initialized" | |
Mediator.broadcast('FakeEvent'); // alerts "Handled 1 times!" (I know, bad grammar) | |
Mediator.broadcast('SetString', ['test string']); // alerts "Assigned test string" | |
Mediator.broadcast('FakeEvent'); // alerts "Handled 2 times!" | |
Mediator.broadcast('SessionStart'); // this call is safely ignored | |
Mediator.broadcast('Translate', ['this is also safely ignored']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment