I am trying to write a class that loads some data and notifies many subscribers that the data is loaded. I have managed to get one subscriber notifed of the event by setting a function to a property in the class, but obviously when the second subscriber sets the notifyMethod, it overwrites the first (see example below). The obvoius solution to me is to hold an array of subscribed functions and provide methods to subscribe and unsubscribe. Before I jump down my own Event subscription implementation, is their any thing in prototype / javascript that allows me to achieve this multiple subscriber events. I noticed the Event class, but that seems to be aimed at DOM events only. Thanks for any help / thoughts. Matt <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script src="/lib/prototype1.5.0/prototype.js" type="text/ javascript"></script> <script src="/lib/scriptaculous/scriptaculous.js" type="text/ javascript"></script> <script type="text/javascript"> TestEventSource = Class.create(); TestEventSource.prototype { initialize: function() { this.notifyMethod = null; this.counter = 0; }, raiseEvent: function() { this.counter++; if(this.notifyMethod) { this.notifyMethod(this.counter); } } }; function Notify1(counter) { $("subscriber1").innerHTML = "subscriber1:" + counter; } function Notify2(counter) { $("subscriber2").innerHTML = "subscriber2:" + counter; } var eventSource = new TestEventSource(); eventSource.notifyMethod = Notify1; // this next line obviously overwrites the method to notify // how do I implement the multiple subscription eventSource.notifyMethod = Notify2; </script> </head> <body> <a href="javascript:eventSource.raiseEvent()">Raise Event</a> <div id="subscriber1">subscriber1</div> <div id="subscriber2">subscriber2</div> </body> </html> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
A couple other guys here have other versions... but here is my custom event pub/sub class: http://wrath.rubyonrails.org/pipermail/rails-spinoffs/2006-February/002875.html And a quick walk through: http://lists.rubyonrails.org/pipermail/rails-spinoffs/2006-March/002885.html On 3/28/07, lummie <blazonhosts-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I am trying to write a class that loads some data and notifies many > subscribers that the data is loaded. I have managed to get one > subscriber notifed of the event by setting a function to a property in > the class, but obviously when the second subscriber sets the > notifyMethod, it overwrites the first (see example below). > > The obvoius solution to me is to hold an array of subscribed functions > and provide methods to subscribe and unsubscribe. > > Before I jump down my own Event subscription implementation, is their > any thing in prototype / javascript that allows me to achieve this > multiple subscriber events. I noticed the Event class, but that seems > to be aimed at DOM events only. > > Thanks for any help / thoughts. > > Matt > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ > TR/html4/strict.dtd"> > <html> > <head> > <meta http-equiv="Content-Type" content="text/html; > charset=iso-8859-1" /> > <title>Untitled Document</title> > <script src="/lib/prototype1.5.0/prototype.js" type="text/ > javascript"></script> > <script src="/lib/scriptaculous/scriptaculous.js" > type="text/ > javascript"></script> > > <script type="text/javascript"> > TestEventSource = Class.create(); > > TestEventSource.prototype > { > initialize: function() > { > this.notifyMethod = null; > this.counter = 0; > }, > > raiseEvent: function() > { > this.counter++; > if(this.notifyMethod) > { > this.notifyMethod(this.counter); > } > } > }; > > function Notify1(counter) > { > $("subscriber1").innerHTML = "subscriber1:" + > counter; > } > > function Notify2(counter) > { > $("subscriber2").innerHTML = "subscriber2:" + > counter; > } > > var eventSource = new TestEventSource(); > eventSource.notifyMethod = Notify1; > // this next line obviously overwrites the method to > notify > // how do I implement the multiple subscription > eventSource.notifyMethod = Notify2; > > > </script> > </head> > <body> > <a href="javascript:eventSource.raiseEvent()">Raise Event</a> > <div id="subscriber1">subscriber1</div> > <div id="subscriber2">subscriber2</div> > </body> > </html> > > > > >-- Ryan Gahl Application Development Consultant Athena Group, Inc. Inquire: 1-920-955-1457 Blog: http://www.someElement.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
And actually I''ve changed is somewhat since that post. My next blog entry (which so far I''m planning to write tomorrow), was actually going to be on this exact subject. I''ll announce that here once I publish it. On 3/28/07, Ryan Gahl <ryan.gahl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > A couple other guys here have other versions... but here is my custom > event pub/sub class: > > http://wrath.rubyonrails.org/pipermail/rails-spinoffs/2006-February/002875.html > > > And a quick walk through: > > > http://lists.rubyonrails.org/pipermail/rails-spinoffs/2006-March/002885.html > > > On 3/28/07, lummie <blazonhosts-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > I am trying to write a class that loads some data and notifies many > > subscribers that the data is loaded. I have managed to get one > > subscriber notifed of the event by setting a function to a property in > > the class, but obviously when the second subscriber sets the > > notifyMethod, it overwrites the first (see example below). > > > > The obvoius solution to me is to hold an array of subscribed functions > > and provide methods to subscribe and unsubscribe. > > > > Before I jump down my own Event subscription implementation, is their > > any thing in prototype / javascript that allows me to achieve this > > multiple subscriber events. I noticed the Event class, but that seems > > to be aimed at DOM events only. > > > > Thanks for any help / thoughts. > > > > Matt > > > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ > > TR/html4/strict.dtd"> > > <html> > > <head> > > <meta http-equiv="Content-Type" content="text/html; > > charset=iso-8859-1" /> > > <title>Untitled Document</title> > > <script src="/lib/prototype1.5.0/prototype.js" > > type="text/ > > javascript"></script> > > <script src="/lib/scriptaculous/scriptaculous.js" > > type="text/ > > javascript"></script> > > > > <script type="text/javascript"> > > TestEventSource = Class.create(); > > > > TestEventSource.prototype > > { > > initialize: function() > > { > > this.notifyMethod = null; > > this.counter = 0; > > }, > > > > raiseEvent: function() > > { > > this.counter++; > > if( this.notifyMethod) > > { > > this.notifyMethod(this.counter); > > } > > } > > }; > > > > function Notify1(counter) > > { > > $("subscriber1").innerHTML = "subscriber1:" + > > counter; > > } > > > > function Notify2(counter) > > { > > $("subscriber2").innerHTML = "subscriber2:" + > > counter; > > } > > > > var eventSource = new TestEventSource(); > > eventSource.notifyMethod = Notify1; > > // this next line obviously overwrites the method to > > notify > > // how do I implement the multiple subscription > > eventSource.notifyMethod = Notify2; > > > > > > </script> > > </head> > > <body> > > <a href="javascript:eventSource.raiseEvent()">Raise Event</a> > > <div id="subscriber1">subscriber1</div> > > <div id="subscriber2">subscriber2</div> > > </body> > > </html> > > > > > > > > > > > > > -- > Ryan Gahl > Application Development Consultant > Athena Group, Inc. > Inquire: 1-920-955-1457 > Blog: http://www.someElement.com-- Ryan Gahl Application Development Consultant Athena Group, Inc. Inquire: 1-920-955-1457 Blog: http://www.someElement.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Are you just trying to observe click events on an anchor? This is built-into rails and supports multiple listeners.... HTML: <a href="foo.html" id="foo">click me</a> JavaScript (both events will fire): Event.observe(''foo'', ''click'', function(){ alert(''clicked!''); }); Event.observe(''foo'', ''click'', function(){ alert(''clicked!''); }); It''s hard to tell from your example whether you are trying to monitor custom events or just normal click events and such. -justin On 3/28/07, lummie <blazonhosts-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am trying to write a class that loads some data and notifies many > subscribers that the data is loaded. I have managed to get one > subscriber notifed of the event by setting a function to a property in > the class, but obviously when the second subscriber sets the > notifyMethod, it overwrites the first (see example below). > > The obvoius solution to me is to hold an array of subscribed functions > and provide methods to subscribe and unsubscribe. > > Before I jump down my own Event subscription implementation, is their > any thing in prototype / javascript that allows me to achieve this > multiple subscriber events. I noticed the Event class, but that seems > to be aimed at DOM events only. > > Thanks for any help / thoughts. > > Matt > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ > TR/html4/strict.dtd"> > <html> > <head> > <meta http-equiv="Content-Type" content="text/html; > charset=iso-8859-1" /> > <title>Untitled Document</title> > <script src="/lib/prototype1.5.0/prototype.js" type="text/ > javascript"></script> > <script src="/lib/scriptaculous/scriptaculous.js" type="text/ > javascript"></script> > > <script type="text/javascript"> > TestEventSource = Class.create(); > > TestEventSource.prototype > { > initialize: function() > { > this.notifyMethod = null; > this.counter = 0; > }, > > raiseEvent: function() > { > this.counter++; > if(this.notifyMethod) > { > this.notifyMethod(this.counter); > } > } > }; > > function Notify1(counter) > { > $("subscriber1").innerHTML = "subscriber1:" + counter; > } > > function Notify2(counter) > { > $("subscriber2").innerHTML = "subscriber2:" + counter; > } > > var eventSource = new TestEventSource(); > eventSource.notifyMethod = Notify1; > // this next line obviously overwrites the method to notify > // how do I implement the multiple subscription > eventSource.notifyMethod = Notify2; > > > </script> > </head> > <body> > <a href="javascript:eventSource.raiseEvent()">Raise Event</a> > <div id="subscriber1">subscriber1</div> > <div id="subscriber2">subscriber2</div> > </body> > </html> > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Custom Events. My own class will raise the events and other custom classes will observe. I''m not trying to observe events from dom elements Sorry if the example is misleading. On Mar 28, 6:05 pm, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Are you just trying to observe click events on an anchor? This is > built-into rails and supports multiple listeners.... > > HTML: > <a href="foo.html" id="foo">click me</a> > > JavaScript (both events will fire): > Event.observe(''foo'', ''click'', function(){ alert(''clicked!''); }); > Event.observe(''foo'', ''click'', function(){ alert(''clicked!''); }); > > It''s hard to tell from your example whether you are trying to monitor > custom events or just normal click events and such. > > -justin > > On 3/28/07, lummie <blazonho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am trying to write a class that loads some data and notifies many > > subscribers that the data is loaded. I have managed to get one > > subscriber notifed of the event by setting a function to a property in > > the class, but obviously when the second subscriber sets the > > notifyMethod, it overwrites the first (see example below). > > > The obvoius solution to me is to hold an array of subscribed functions > > and provide methods to subscribe and unsubscribe. > > > Before I jump down my own Event subscription implementation, is their > > any thing in prototype / javascript that allows me to achieve this > > multiple subscriber events. I noticed the Event class, but that seems > > to be aimed at DOM events only. > > > Thanks for any help / thoughts. > > > Matt > > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ > > TR/html4/strict.dtd"> > > <html> > > <head> > > <meta http-equiv="Content-Type" content="text/html; > > charset=iso-8859-1" /> > > <title>Untitled Document</title> > > <script src="/lib/prototype1.5.0/prototype.js" type="text/ > > javascript"></script> > > <script src="/lib/scriptaculous/scriptaculous.js" type="text/ > > javascript"></script> > > > <script type="text/javascript"> > > TestEventSource = Class.create(); > > > TestEventSource.prototype > > { > > initialize: function() > > { > > this.notifyMethod = null; > > this.counter = 0; > > }, > > > raiseEvent: function() > > { > > this.counter++; > > if(this.notifyMethod) > > { > > this.notifyMethod(this.counter); > > } > > } > > }; > > > function Notify1(counter) > > { > > $("subscriber1").innerHTML = "subscriber1:" + counter; > > } > > > function Notify2(counter) > > { > > $("subscriber2").innerHTML = "subscriber2:" + counter; > > } > > > var eventSource = new TestEventSource(); > > eventSource.notifyMethod = Notify1; > > // this next line obviously overwrites the method to notify > > // how do I implement the multiple subscription > > eventSource.notifyMethod = Notify2; > > > </script> > > </head> > > <body> > > <a href="javascript:eventSource.raiseEvent()">Raise Event</a> > > <div id="subscriber1">subscriber1</div> > > <div id="subscriber2">subscriber2</div> > > </body> > > </html>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
looking forward to it... please let me know when it''s published. In the meantime I''ll have a look at the EventPublisher class. On Mar 28, 6:04 pm, "Ryan Gahl" <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> And actually I''ve changed is somewhat since that post. My next blog entry > (which so far I''m planning to write tomorrow), was actually going to be on > this exact subject. I''ll announce that here once I publish it. > > On 3/28/07, Ryan Gahl <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > A couple other guys here have other versions... but here is my custom > > event pub/sub class: > > >http://wrath.rubyonrails.org/pipermail/rails-spinoffs/2006-February/0... > > > And a quick walk through: > > >http://lists.rubyonrails.org/pipermail/rails-spinoffs/2006-March/0028... > > > On 3/28/07, lummie <blazonho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I am trying to write a class that loads some data and notifies many > > > subscribers that the data is loaded. I have managed to get one > > > subscriber notifed of the event by setting a function to a property in > > > the class, but obviously when the second subscriber sets the > > > notifyMethod, it overwrites the first (see example below). > > > > The obvoius solution to me is to hold an array of subscribed functions > > > and provide methods to subscribe and unsubscribe. > > > > Before I jump down my own Event subscription implementation, is their > > > any thing in prototype / javascript that allows me to achieve this > > > multiple subscriber events. I noticed the Event class, but that seems > > > to be aimed at DOM events only. > > > > Thanks for any help / thoughts. > > > > Matt > > > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ > > > TR/html4/strict.dtd"> > > > <html> > > > <head> > > > <meta http-equiv="Content-Type" content="text/html; > > > charset=iso-8859-1" /> > > > <title>Untitled Document</title> > > > <script src="/lib/prototype1.5.0/prototype.js" > > > type="text/ > > > javascript"></script> > > > <script src="/lib/scriptaculous/scriptaculous.js" > > > type="text/ > > > javascript"></script> > > > > <script type="text/javascript"> > > > TestEventSource = Class.create(); > > > > TestEventSource.prototype > > > { > > > initialize: function() > > > { > > > this.notifyMethod = null; > > > this.counter = 0; > > > }, > > > > raiseEvent: function() > > > { > > > this.counter++; > > > if( this.notifyMethod) > > > { > > > this.notifyMethod(this.counter); > > > } > > > } > > > }; > > > > function Notify1(counter) > > > { > > > $("subscriber1").innerHTML = "subscriber1:" + > > > counter; > > > } > > > > function Notify2(counter) > > > { > > > $("subscriber2").innerHTML = "subscriber2:" + > > > counter; > > > } > > > > var eventSource = new TestEventSource(); > > > eventSource.notifyMethod = Notify1; > > > // this next line obviously overwrites the method to > > > notify > > > // how do I implement the multiple subscription > > > eventSource.notifyMethod = Notify2; > > > > </script> > > > </head> > > > <body> > > > <a href="javascript:eventSource.raiseEvent()">Raise Event</a> > > > <div id="subscriber1">subscriber1</div> > > > <div id="subscriber2">subscriber2</div> > > > </body> > > > </html> > > > -- > > Ryan Gahl > > Application Development Consultant > > Athena Group, Inc. > > Inquire: 1-920-955-1457 > > Blog:http://www.someElement.com > > -- > Ryan Gahl > Application Development Consultant > Athena Group, Inc. > Inquire: 1-920-955-1457 > Blog:http://www.someElement.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---