Event handling

Hello,

I am confused about certain behavior regarding event handling. Specifically, whenever I am creating a kind that inherits from another kind which has some events declared in the events block, I can not directly assign handlers of these events as properties for the kind I am about to create. I can only use the hanlers block as a catch-all of the events of that type in order to handle the base kind's events. Maybe what I am writing right now wont make much sense without an example so here it is one. http://jsfiddle.net/dimitrk/7L87E/

Comments

  • edited March 2014
    The purpose of setting a event handler as a property on the instance is to allow the owner of the instance (and not the instance itself) to handle the event.

    Given this code from your example:
    enyo.kind({
        name: "example.inheritanceInMainKind",
        kind: "example.base",
        onEventHappent: "handleEvent",
        handlers: {
            onEventHappent: "handleHandler"
        }
    });
    When onEventHappent is triggered, enyo will call handleHandler on the instance of this component because its in the handlers block and will call handlEvent on the owner of the instance. You can step through the logic in enyo.Component.dispatchEvent to see this in action.

    In general, you shouldn't need to "handle" an event from your own kind. Sometimes, though, you might want to subkind a framework component and handle its events rather than override private methods. In this case, using the handlers block is the right way to do it.
Sign In or Register to comment.