Loading ...
Sorry, an error occurred while loading the content.

Working with events in non-YUI class

Expand Messages
  • rballou05
    I m creating a widget that I would like to use YUI s event model and support. So I have something like this: MyClass = function(){} MyClass.prototype = { foo:
    Message 1 of 2 , Nov 1, 2007
    • 0 Attachment
      I'm creating a widget that I would like to use YUI's event model and
      support. So I have something like this:


      MyClass = function(){}
      MyClass.prototype = {
      foo: function(){
      this.fireEvent("foo")
      return "Foo!";
      }
      }
      YAHOO.lang.augment(MyClass, YAHOO.util.EventProvider)

      mc = new MyClass()
      mc.subscribe("foo", function(){ alert("Test") })
      mc.foo()


      Unfortunately, while mc.subscribe() does not throw an error, it does
      not trigger the alert() like I want. The only thing that I have got to
      work is using CustomEvents like this:


      MyClass.prototype = {
      foo: function(){
      this.fooEvent.fire()
      return "Foo!"
      },
      fooEvent: new YAHOO.util.CustomEvent('fooEvent')
      }
      YAHOO.lang.augment(MyClass, YAHOO.util.EventProvider)
      mc = new MyClass()
      mc.fooEvent.subscribe(function(){ alert("test") })


      While this behaves the way I want, I'm not happy with the syntax and
      I feel like I am just missing something... I would like the first
      syntax to work because I don't feel like users (of my widget) should
      have to subscribe by naming the event like in the second example (and
      I don't particularly like creating a new CustomEvent for each event I
      would like to fire).

      Thanks in advance!

      Rob
    • y_lsmith
      ... Rob, EventProvider s fireEvent currently only works for events that were created via the createEvent(type,config) method. In your MyClass constructor, you
      Message 2 of 2 , Nov 8, 2007
      • 0 Attachment
        --- In ydn-javascript@yahoogroups.com, "rballou05" <rob.ballou@...> wrote:
        >
        > I'm creating a widget that I would like to use YUI's event model and
        > support. So I have something like this:
        >
        >
        > MyClass = function(){}
        > MyClass.prototype = {
        >   foo: function(){
        >     this.fireEvent("foo")
        >     return "Foo!";
        >   }
        > }
        > YAHOO.lang.augment(MyClass, YAHOO.util.EventProvider)
        >
        > mc = new MyClass()
        > mc.subscribe("foo", function(){ alert("Test") })
        > mc.foo()
        >
        >
        > Unfortunately, while mc.subscribe() does not throw an error, it does
        > not trigger the alert() like I want. The only thing that I have got to
        > work is using CustomEvents like this:
        >
        >
        > MyClass.prototype = {
        >   foo: function(){
        >     this.fooEvent.fire()
        >     return "Foo!"
        >   },
        >   fooEvent: new YAHOO.util.CustomEvent('fooEvent')
        > }
        > YAHOO.lang.augment(MyClass, YAHOO.util.EventProvider)
        > mc = new MyClass()
        > mc.fooEvent.subscribe(function(){ alert("test") })
        >
        >
        > While this behaves the way I want, I'm not happy with the syntax and
        > I feel like I am just missing something... I would like the first
        > syntax to work because I don't feel like users (of my widget) should
        > have to subscribe by naming the event like in the second example (and
        > I don't particularly like creating a new CustomEvent for each event I
        > would like to fire).
        >
        > Thanks in advance!
        >
        > Rob
        >

        Rob,

        EventProvider's fireEvent currently only works for events that were
        created via the createEvent(type,config) method.

        In your MyClass constructor, you can simply add

        function MyClass() {
          ...
          this.createEvent('foo');
          ...
        }

        and the rest of your code should work as desired.

        Incidentally, take a look at this example for YAHOO.lang.augmentProto


        Hope this helps,
        Luke
      Your message has been successfully submitted and would be delivered to recipients shortly.