Created
July 26, 2016 10:22
-
-
Save anonymous/b20903d164824ccb88bc3f5b94b706cb to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/lojexi
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
console.clear(); | |
var mediator = (function(){ | |
// Storage for our topics/events | |
var channels = {}; | |
// Subscribe to an event, supply a callback to be executed | |
// when that event is broadcast | |
var subscribe = function(channel, fn){ | |
if (!channels[channel]) channels[channel] = []; | |
channels[channel].push({ context: this, callback: fn }); | |
return this; | |
}; | |
// Publish/broadcast an event to the rest of the application | |
var publish = function(channel){ | |
if (!channels[channel]) return false; | |
var args = Array.prototype.slice.call(arguments, 1); | |
for (var i = 0, l = channels[channel].length; i < l; i++) { | |
var subscription = channels[channel][i]; | |
subscription.callback.apply(subscription.context, args); | |
} | |
return this; | |
}; | |
return { | |
publish: publish, | |
subscribe: subscribe, | |
installTo: function(obj){ | |
obj.subscribe = subscribe; | |
obj.publish = publish; | |
} | |
}; | |
}()); | |
(function( m ){ | |
var m2 = {}; | |
m.installTo(m2); | |
// Set a default value for 'person' | |
var person = "Luke"; | |
// Subscribe to a topic/event called 'nameChange' with | |
// a callback function which will log the original | |
// person's name and (if everything works) the incoming | |
// name | |
m2.subscribe('nameChange', function( arg ){ | |
console.log( person ); // Luke | |
person = arg; | |
console.log( person ); // David | |
}); | |
// Publish the 'nameChange' topic/event with the new data | |
m2.publish( 'nameChange', 'David' ); | |
})( mediator ); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.clear(); | |
var mediator = (function(){ | |
// Storage for our topics/events | |
var channels = {}; | |
// Subscribe to an event, supply a callback to be executed | |
// when that event is broadcast | |
var subscribe = function(channel, fn){ | |
if (!channels[channel]) channels[channel] = []; | |
channels[channel].push({ context: this, callback: fn }); | |
return this; | |
}; | |
// Publish/broadcast an event to the rest of the application | |
var publish = function(channel){ | |
if (!channels[channel]) return false; | |
var args = Array.prototype.slice.call(arguments, 1); | |
for (var i = 0, l = channels[channel].length; i < l; i++) { | |
var subscription = channels[channel][i]; | |
subscription.callback.apply(subscription.context, args); | |
} | |
return this; | |
}; | |
return { | |
publish: publish, | |
subscribe: subscribe, | |
installTo: function(obj){ | |
obj.subscribe = subscribe; | |
obj.publish = publish; | |
} | |
}; | |
}()); | |
(function( m ){ | |
var m2 = {}; | |
m.installTo(m2); | |
// Set a default value for 'person' | |
var person = "Luke"; | |
// Subscribe to a topic/event called 'nameChange' with | |
// a callback function which will log the original | |
// person's name and (if everything works) the incoming | |
// name | |
m2.subscribe('nameChange', function( arg ){ | |
console.log( person ); // Luke | |
person = arg; | |
console.log( person ); // David | |
}); | |
// Publish the 'nameChange' topic/event with the new data | |
m2.publish( 'nameChange', 'David' ); | |
})( mediator );</script></body> | |
</html> |
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
console.clear(); | |
var mediator = (function(){ | |
// Storage for our topics/events | |
var channels = {}; | |
// Subscribe to an event, supply a callback to be executed | |
// when that event is broadcast | |
var subscribe = function(channel, fn){ | |
if (!channels[channel]) channels[channel] = []; | |
channels[channel].push({ context: this, callback: fn }); | |
return this; | |
}; | |
// Publish/broadcast an event to the rest of the application | |
var publish = function(channel){ | |
if (!channels[channel]) return false; | |
var args = Array.prototype.slice.call(arguments, 1); | |
for (var i = 0, l = channels[channel].length; i < l; i++) { | |
var subscription = channels[channel][i]; | |
subscription.callback.apply(subscription.context, args); | |
} | |
return this; | |
}; | |
return { | |
publish: publish, | |
subscribe: subscribe, | |
installTo: function(obj){ | |
obj.subscribe = subscribe; | |
obj.publish = publish; | |
} | |
}; | |
}()); | |
(function( m ){ | |
var m2 = {}; | |
m.installTo(m2); | |
// Set a default value for 'person' | |
var person = "Luke"; | |
// Subscribe to a topic/event called 'nameChange' with | |
// a callback function which will log the original | |
// person's name and (if everything works) the incoming | |
// name | |
m2.subscribe('nameChange', function( arg ){ | |
console.log( person ); // Luke | |
person = arg; | |
console.log( person ); // David | |
}); | |
// Publish the 'nameChange' topic/event with the new data | |
m2.publish( 'nameChange', 'David' ); | |
})( mediator ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment