Skip to content

Commit

Permalink
- uses overloadSetter to simplify the script while providing the same…
Browse files Browse the repository at this point in the history
… API.

- for use on the server, Element module is now optional
  • Loading branch information
ryanflorence committed Oct 25, 2010
1 parent a191a3d commit 94d54fd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 50 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -72,6 +72,12 @@ Todo


Unsubscribe an object from a channel with `unsubscribe`, but keep other element subscriptions in tact. Unsubscribe an object from a channel with `unsubscribe`, but keep other element subscriptions in tact.


Works on the Server and the Client
==================================

Mediating element events is optional, making this script ideal for both server-side and client-side scripts. You need to include `Element.Channels` if you want to mediate element events.


MooTools and Publish Subscribe MooTools and Publish Subscribe
============================== ==============================


Expand Down
62 changes: 21 additions & 41 deletions Source/Channels.js
Expand Up @@ -3,7 +3,7 @@
name: Channels name: Channels
description: Mediate Element and Class events through the window. An expanded pattern for pub/sub. description: Mediate Class events. An expanded pattern for pub/sub.
license: MIT-style license. license: MIT-style license.
Expand All @@ -16,7 +16,6 @@ inspiration:
requires: requires:
- Core/Class.Extras - Core/Class.Extras
- Core/Element.Event
provides: [Channels] provides: [Channels]
Expand All @@ -25,47 +24,25 @@ provides: [Channels]


(function(){ (function(){


var mediator = new Events var mediator = new Events,


, methods = { methods = {

publishes: function(channel, eventName){
createChannel: function(channel, eventName){ Channels.publishing.push({publisher: this, channel: channel, event: eventName});
Channels.publishing.push({publisher: this, channel: channel, event: eventName}); this.addEvent(eventName, function(){
this.addEvent(eventName, function(){ mediator.fireEvent.call(mediator, channel, arguments);
mediator.fireEvent.call(mediator, channel, arguments); });
}); return this;
return this; }.overloadSetter(), // not public MooTools
},

subscribe: function(channel, fn){
createChannels: function(events){ Channels.subscriptions.push({subscriber: this, channel: channel});
for (var channel in events) this.createChannel(channel, events[channel]); mediator.addEvent(channel, fn.bind(this));
return this; return this;
}, }.overloadSetter()

publishes: function(){
return this['createChannel' + ((arguments.length == 1) ? 's' : '')].apply(this, arguments);
},

subscribeToChannel: function(channel, fn){
Channels.subscriptions.push({subscriber: this, channel: channel});
mediator.addEvent(channel, fn.bind(this));
return this;
}, },

subscribeToChannels: function(events){
for (var channel in events) this.subscribeToChannel(channel, events[channel]);
return this;
},

subscribe: function(){
return this['subscribeToChannel' + ((arguments.length == 1) ? 's' : '')].apply(this, arguments);
}

}


, Channels = this.Channels = new Class(methods); Channels = this.Channels = new Class(methods);

this.Element.implement(methods);


Object.append(Channels, { Object.append(Channels, {
publishing: [], publishing: [],
Expand All @@ -75,6 +52,9 @@ Object.append(Channels, {
Channels.publishing = Channels.publishing.filter(function(item){ Channels.publishing = Channels.publishing.filter(function(item){
return item.channel != channel; return item.channel != channel;
}); });
},
installTo: function(obj){
obj.implement(methods);
} }
}); });


Expand Down
27 changes: 27 additions & 0 deletions Source/Element.Channels.js
@@ -0,0 +1,27 @@
/*
---
name: Element.Channels
description: Mediate Element events. An expanded pattern for pub/sub.
license: MIT-style license.
copyright: Copyright (c) 2010 [Ryan Florence](http://ryanflorence.com/).
authors: Ryan Florence
requires:
- /Channels
- Core/Element.Event
provides: [Element.Channels]
...
*/

[Window, Document, Element].each(function(item){
Channels.installTo(item);
});


14 changes: 6 additions & 8 deletions Tests/Channels/Channels.html
Expand Up @@ -14,27 +14,25 @@


<div id=fixture>Waiting...</div> <div id=fixture>Waiting...</div>


<script src="/depender/build?require=Channels/Channels,Core/Fx.Tween"></script> <script src="/depender/build?require=Channels/Element.Channels,Core/Fx.Tween"></script>


<script> <script>
Fx.implement(Channels.prototype); Fx.implement(Channels.prototype);


var fixture = $('fixture').subscribe({ var fixture = $('fixture').subscribe({
'/fx/start': function(){ '/fx/start': function(){
this.set('text', 'oh cool, the fx started') this.set('text', 'oh cool, the fx started')
},
'/fx/complete': function(){
this.set('text', "All done.").set.delay(1000, this, ['text','Waiting...']);
} }
}).subscribe('/fx/complete', function(){
this.set('text', "All done.").set.delay(1000, this, ['text','Waiting...']);
}); });


var fx = new Fx.Tween(fixture, { var fx = new Fx.Tween(fixture, {
property: 'background-color', property: 'background-color',
duration: 2000 duration: 2000
}).publishes( { }).publishes({
'/fx/start': 'start', '/fx/start': 'start'
'/fx/complete': 'complete' }).publishes('/fx/complete', 'complete');
});


makeActions([ makeActions([


Expand Down
4 changes: 3 additions & 1 deletion package.yml
Expand Up @@ -7,4 +7,6 @@ copyright: "&copy; [Ryan Florence](http://ryanflorence.com)"
license: MIT-style license. license: MIT-style license.


sources: sources:
- "Source/Channels.js" - "Source/Channels.js"
- "Source/Element.Channels.js"

0 comments on commit 94d54fd

Please sign in to comment.