Keith,
that's very simple:

var Puppy = Class.create({
  initialize: function(name) {
    this.name = name;
  },
  bark: function () {
    console.log(this.name + ": Woof!");
    document.fire('puppy:bark', { name: this.name });
  }
});


var Kitten = Class.create({
  initialize: function (name) {
    this.name = name;
    document.observe('puppy:bark',
this.onBark.bindAsEventListener(this));
  },
  onBark: function (e) {
    console.log(this.name + ": Why hello der " + e.memo.name + " !");
  }
});

var otis = new Puppy("Otis");
var milo = new Kitten("Milo");

otis.bark();

/*

Otis: Woof!
Milo: Why hello der Otis !

*/

Best,
kangax

On Mar 4, 4:36 pm, Keith <[EMAIL PROTECTED]> wrote:
> To give another example of what I was thinking, using YUI's custom
> event object (just an example..) you can do:
>
> // Begin Code...
>
>         var Puppy = Class.create();
>
>         //Create and a custom event
>         document.barkEvent = new YAHOO.util.CustomEvent("puppyBark", this);
>
>         Puppy.prototype = {
>                 initialize: function (name) {
>                         this.name = name;
>                 },
>                 bark: function () {
>                         console.log(this.name + ": Woof!");
>
>                         //Fire event
>                         document.barkEvent.fire(this);
>                 }
>         }
>
>         var Kitten = Class.create();
>
>         Kitten.prototype = {
>                 initialize: function (name) {
>                         this.name = name;
>
>                         //Subscripe to bark event
>                         document.barkEvent.subscribe(this.onBark, this);
>                 },
>
>                 onBark: function (type, args, me) {
>                         console.log(me.name + ": Why hello der" + 
> args[0].name + " !");
>                 }
>         }
>
>         //Test:
>         var otis = new Puppy("Otis");
>         var milo = new Kitten("Milo");
>
>         otis.bark();
>
> // End Code...
>
> ---------- OUTPUT ------------
> Otis: Woof!
> Milo: Why hello der Otis!
> --------------------------------------
>
> -Keith
>
> On Mar 4, 2:07 pm, Keith <[EMAIL PROTECTED]> wrote:> Thanks for the reply. 
> That looks like an interesting route of handling
> > events, but
> > not exactly what I had in mind. Ideally I would like to be able to
> > just use Event#observe
> > and Event#fire. It might be workable using wrap, but that would add a
> > whole other
> > layer of complexity that I hope to avoid.
>
> > Thanks,
> > Keith
>
> > On Mar 2, 5:16 am, kangax <[EMAIL PROTECTED]> wrote:
>
> > > #wrap is simply amazing. We can easily augment Class.create to make
> > > all of instance methods fire proper events ( e.g. "Class:method" )
>
> > > Class.create = Class.create.wrap(function() {
> > >   var args = $A(arguments),
> > >       proceed = args.shift(),
> > >       methods = args.last(),
> > >       className = methods['className'] || 'Klass';
> > >   for (var m in methods) {
> > >     if (/initialize|className/.test(m)) continue;
> > >     methods[m] = methods[m].wrap(function() {
> > >       var _args = $A(arguments), _proceed = _args.shift();
> > >       console.log(className + ':' + m);
> > >       return _proceed.apply(_proceed, _args);
> > >     })
> > >   }
> > >   return proceed.apply(proceed, args);
>
> > > })
>
> > > var Class1 = Class.create({
> > >   initialize: function() { },
> > >   className: 'Class1',
> > >   bar: function() { }
>
> > > });
>
> > > var Class2 = Class.create({
> > >   initialize: function() { },
> > >   className: 'Class2',
> > >   baz: function() { }
>
> > > });
>
> > > new Class1().bar(); // fires "Class1:bar"
> > > new Class2().baz(); // fires "Class2:baz"
>
> > > P.S. This is by no means a thorough solution, but rather a proof of
> > > concept...
>
> > > Best,
> > > kangax
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to