Permalink
Browse files

- uses overloadSetter to simplify the script while providing the same…

… API.

- for use on the server, Element module is now optional
  • Loading branch information...
1 parent a191a3d commit 94d54fd02cc902bfedeaf4372ef7a6e2b082754b @ryanflorence committed Oct 25, 2010
Showing with 63 additions and 50 deletions.
  1. +6 −0 README.md
  2. +21 −41 Source/Channels.js
  3. +27 −0 Source/Element.Channels.js
  4. +6 −8 Tests/Channels/Channels.html
  5. +3 −1 package.yml
View
@@ -72,6 +72,12 @@ Todo
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
==============================
View
@@ -3,7 +3,7 @@
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.
@@ -16,7 +16,6 @@ inspiration:
requires:
- Core/Class.Extras
- - Core/Element.Event
provides: [Channels]
@@ -25,47 +24,25 @@ provides: [Channels]
(function(){
-var mediator = new Events
-
-, methods = {
-
- createChannel: function(channel, eventName){
- Channels.publishing.push({publisher: this, channel: channel, event: eventName});
- this.addEvent(eventName, function(){
- mediator.fireEvent.call(mediator, channel, arguments);
- });
- return this;
- },
-
- createChannels: function(events){
- for (var channel in events) this.createChannel(channel, events[channel]);
- return this;
- },
-
- 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;
+var mediator = new Events,
+
+ methods = {
+ publishes: function(channel, eventName){
+ Channels.publishing.push({publisher: this, channel: channel, event: eventName});
+ this.addEvent(eventName, function(){
+ mediator.fireEvent.call(mediator, channel, arguments);
+ });
+ return this;
+ }.overloadSetter(), // not public MooTools
+
+ subscribe: function(channel, fn){
+ Channels.subscriptions.push({subscriber: this, channel: channel});
+ mediator.addEvent(channel, fn.bind(this));
+ return this;
+ }.overloadSetter()
},
-
- 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);
-
-this.Element.implement(methods);
+ Channels = this.Channels = new Class(methods);
Object.append(Channels, {
publishing: [],
@@ -75,6 +52,9 @@ Object.append(Channels, {
Channels.publishing = Channels.publishing.filter(function(item){
return item.channel != channel;
});
+ },
+ installTo: function(obj){
+ obj.implement(methods);
}
});
View
@@ -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,27 +14,25 @@
<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>
Fx.implement(Channels.prototype);
var fixture = $('fixture').subscribe({
'/fx/start': function(){
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, {
property: 'background-color',
duration: 2000
-}).publishes( {
- '/fx/start': 'start',
- '/fx/complete': 'complete'
-});
+}).publishes({
+ '/fx/start': 'start'
+}).publishes('/fx/complete', 'complete');
makeActions([
View
@@ -7,4 +7,6 @@ copyright: "&copy; [Ryan Florence](http://ryanflorence.com)"
license: MIT-style license.
sources:
- - "Source/Channels.js"
+ - "Source/Channels.js"
+ - "Source/Element.Channels.js"
+

0 comments on commit 94d54fd

Please sign in to comment.