#58 ✓not_for_core
John-David Dalton

Add "fire" and "observe" to Class.Methods

Reported by John-David Dalton | April 28th, 2008 @ 02:48 AM

Juriy has given classes the ability to fire events via a few lines of code:

http://github.com/kangax/protoli...

I think we could take this a step further and allow

Class.Methods to contain methods that are auto mixed in for Classes when used via Class.create().

I think that the default methods on Class.Methods could be "fire" and "observe".

If later, devs wanted to do something like:

Object.extend(Class.Methods, CustomMixin);

All classes created after that would have CustomMixin mixed-in automatically.

Comments and changes to this ticket

  • John-David Dalton

    John-David Dalton April 28th, 2008 @ 02:29 AM

    • State changed from “new” to “enhancement”
  • Juriy Zaytsev

    Juriy Zaytsev April 28th, 2008 @ 02:54 AM

    Just a couple of thoughts:

    1) Class.Observable mixin should not be added to objects (created via Class.create) automagically - injecting all objects with 3 more methods sounds too "bloatish".

    2) "observe" on instance object is not the same "observe" as the one on a class object. "observe" on a class object should "catch" events from any of its instances (that's how Livepipe's Object.Event does it)

    3) Still not sure how all of this should work with subclasses - i.e. does class catch events of its subclass instances?

  • John-David Dalton

    John-David Dalton April 28th, 2008 @ 03:12 AM

    Jury,

    I don't think Class.Observable should exist.

    Instead move those methods to Class.Methods.

    I disagree with number 2.

    I think observe should be rather simple.

    var Foo = Class.create();
    Foo.observe('customevent', function(){ ...});
    Foo.fire('customevent'); //will not fire to all instances, scope of this -> Foo.
    
    var Bar = new Foo();
    Bar.observe('somethingelse', function(b){...});
    Bar.fire('somethingelse'); // scope of this -> Bar.
    

    If you want all instances to respond to an event

    from the constructor you could do:

    var Foo = Class.create({
      initialize: function(){
        Foo.observe('customevent');
      }
    });
    

    then Foo.fire('customevent') would go to all the children.

    On 3, I think event bubbling needs further discussion.

  • Juriy Zaytsev

    Juriy Zaytsev April 28th, 2008 @ 03:21 AM

    Well, you don't really have control of a class' constructor. If I write some widget and user wants to observe all instances of that widget - he/she would need to override (wrap) my constructor to support class level observing.

    This doesn't sound like a good idea : )

  • John-David Dalton

    John-David Dalton April 28th, 2008 @ 04:40 AM

    Good point, though I still stand by my post stating that there should be a Class.Methods and that methods on Class.Methods should be automatically added to classes created. Also that "fire" and "observe" should be the default methods on Class.Methods.

    Further discussion is needed to work out the details about event bubbling and constructors broadcasting to instances.

  • John-David Dalton
  • John-David Dalton

    John-David Dalton April 29th, 2008 @ 06:12 PM

    • Title changed from “Class.Methods (for automatic mixins)” to “Add "fire" and "observe" to Class.Methods”

    Duh, just looked at the source and Prototype already uses Class.Methods for auto mixins.

    So I guess this would be to just add "fire" and "observe" to them :)

  • John-David Dalton

    John-David Dalton April 30th, 2008 @ 11:12 PM

    • State changed from “enhancement” to “not_for_core”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Pages