I got a lot of great help yesterday on my calendar questions. Makes me want to post another contribution :-) This one is called EventPublisher. This was packaged also in Marco Jaeger''s recent post with his excellent Dialogs (a la Windows), but now I''ll post it by itself in case someone missed it, and also give a little explanation for ya''ll. This class allows you to very easily fire and subscribe to events from any object. It allows you to attach event handlers both synchronously and asynchronously, attach any number of event handlers to a specific event, and pass any arguments to those handlers that you want. The file is attached (sans the ".js" extension to get past my company''s firewall, just re-add that once you have the file, replacing the ".txt"). USAGE: Let''s say you have an object called BouncingBall, and you''d like to fire notification events every time the ball bounces. To enable this, just extend BouncingBall to inherit from EventPublisher, and then use the fireEvent method... Object.extend(Object.extend(BouncingBall.prototype, EventPublisher.prototype), { initialize: function(color) { this.color = color; ...other setup stuff and some code the makes the ball start bouncing... }, _bounce: function(howHigh) { //...fire the onBounce event with no arguments... this.fireEvent( "onBounce" ); //...or fire it with some arguments... this.fireEvent( "onBounce", {ball: this, howHigh: howHigh} ); ...make the ball bounce here... } }); Then let''s say you have a class called Floor that tracks all your BouncingBalls, and needs to know when they bounce so it can push back on them (you know, for every action there is an equal and opposite reaction)... Floor.prototype { initialize: function(balls) { this.balls = []; //if an initial balls collection was passed in, loop through it, adding each ball separately in order to setup the event handlers... if (balls) $A(balls).each(function(b) { this.addBall(b); }.bind(this)); }, addBall: function(ball) { this.balls.push(ball); // set up the event handler for this ball (synchronously, so no 3rd parameter)... ball.attachEventHandler( "onBounce", this.ballBounce.bind(this) ); // ...or set it up so it executes asynchronously (pass in "true" as the 3rd parameter) // obviously a real floor can handle many balls bouncing at the same time, so this is probably the way to go ball.attachEventHandler( "onBounce", this.ballBounce.bind(this), true ); }, ballBounce: function(args) { // assuming arguments are passed (the 2nd fireEvent is used from the 1st example)... // special physics... red balls bounce twice as high :-) if (args.ball.color == "red") this.doTheBounceStuff(args.ball, args.howHigh * 2); else this.doTheBounceStuff(args.ball, args.howHigh); }, doTheBounceStuff: function(ball, howHigh) { ...push back on the ball to make a bunch of dead physicists proud... } }; Please let me know if you have any questions about this class... Most of the other methods should be pretty self-explanatory though. You should be able to drop it in and be on your way to a consistent, robust event model for all your objects :-) Enjoy! Sincerely, Ryan Gahl Design Engineer Camtronics Medical Systems (an Emageon Company) Ryan.gahl-nlycWCgr5/vuufBYgWm87A@public.gmane.org 262-369-3251 The information transmitted in this electronic mail is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Thank you a lot for explanations ! 8) On 2/28/06, Ryan Gahl <Ryan.Gahl-nlycWCgr5/vuufBYgWm87A@public.gmane.org> wrote:> > I got a lot of great help yesterday on my calendar questions. Makes me want > to post another contribution :-) > > This one is called EventPublisher. This was packaged also in Marco Jaeger''s > recent post with his excellent Dialogs (a la Windows), but now I''ll post it > by itself in case someone missed it, and also give a little explanation for > ya''ll. > > This class allows you to very easily fire and subscribe to events from any > object. It allows you to attach event handlers both synchronously and > asynchronously, attach any number of event handlers to a specific event, and > pass any arguments to those handlers that you want. The file is attached > (sans the ".js" extension to get past my company''s firewall, just re-add > that once you have the file, replacing the ".txt"). > > USAGE: > > Let''s say you have an object called BouncingBall, and you''d like to fire > notification events every time the ball bounces. To enable this, just extend > BouncingBall to inherit from EventPublisher, and then use the fireEvent > method... > > Object.extend(Object.extend(BouncingBall.prototype, > EventPublisher.prototype), > { > initialize: function(color) > { > this.color = color; > ...other setup stuff and some code the makes the ball start > bouncing... > }, > > _bounce: function(howHigh) > { > //...fire the onBounce event with no arguments... > this.fireEvent( "onBounce" ); > > //...or fire it with some arguments... > this.fireEvent( "onBounce", {ball: this, howHigh: howHigh} ); > > ...make the ball bounce here... > } > }); > > > Then let''s say you have a class called Floor that tracks all your > BouncingBalls, and needs to know when they bounce so it can push back on > them (you know, for every action there is an equal and opposite reaction)... > > Floor.prototype > { > initialize: function(balls) > { > this.balls = []; > > //if an initial balls collection was passed in, loop through it, > adding each ball separately in order to setup the event handlers... > if (balls) > $A(balls).each(function(b) { this.addBall(b); }.bind(this)); > }, > > addBall: function(ball) > { > this.balls.push(ball); > > // set up the event handler for this ball (synchronously, so no 3rd > parameter)... > ball.attachEventHandler( "onBounce", this.ballBounce.bind(this) ); > > // ...or set it up so it executes asynchronously (pass in "true" as > the 3rd parameter) > // obviously a real floor can handle many balls bouncing at the same > time, so this is probably the way to go > ball.attachEventHandler( "onBounce", this.ballBounce.bind(this), > true ); > }, > > ballBounce: function(args) > { > // assuming arguments are passed (the 2nd fireEvent is used from the > 1st example)... > > // special physics... red balls bounce twice as high :-) > if (args.ball.color == "red") > this.doTheBounceStuff(args.ball, args.howHigh * 2); > else > this.doTheBounceStuff(args.ball, args.howHigh); > }, > > doTheBounceStuff: function(ball, howHigh) > { > ...push back on the ball to make a bunch of dead physicists proud... > } > }; > > > Please let me know if you have any questions about this class... Most of the > other methods should be pretty self-explanatory though. You should be able > to drop it in and be on your way to a consistent, robust event model for all > your objects :-) Enjoy! > > > > > > Sincerely, > > Ryan Gahl > > Design Engineer > > Camtronics Medical Systems (an Emageon Company) > > Ryan.gahl-nlycWCgr5/vuufBYgWm87A@public.gmane.org > > 262-369-3251 > > > The information transmitted in this electronic mail is intended only for the > person or entity to which it is addressed and may contain confidential, > proprietary, and/or privileged material. Any review, retransmission, > dissemination or other use of, or taking of any action in reliance upon, > this information by persons or entities other than the intended recipient is > prohibited. If you received this in error, please contact the sender and > delete the material from all computers. > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > >
> This one is called EventPublisher.Cool, very useful. Although I won''t remodel my current project to use it :-)
Well, then, maybe future projects. :-) The information transmitted in this electronic mail is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers.