domo icon indicating copy to clipboard operation
domo copied to clipboard

Add support for events

Open ghost opened this issue 12 years ago • 5 comments

/*
  Event bindings are defined as mappings of event names to simple
  objects which consist of the event callback, the context to bind
  'this' to for the event and perhaps additional arguments relative to
  the event that should be passed to the handler.

  The callback signature would be function (eventName, element, eventArgs)

  Maybe a mixin could be created that utilizes one of the many event
  micro libraries on microjs.com could be used to make this an
  optional extension, modular and also relieve the library of having
  to worry about cross browser event support.

  My reasoning is that since dom-o is creating the elements, it could
  easily attach events as it is creating the elements as opposed to
  doing some kind of CSS selector based lookup after the fact in order
  to find the element again to attach the event that way. It also
  helps keep the beauty of the DSL in tact by not requiring elements
  that need events attached to them to be instantiated separately and
  then composed into a broader dom-o statement.
*/

FORM({action: "post_here.py", method: "POST"},
  LABEL({for: "name"}, "E-mail:"),
  INPUT({id: "name", name: "name", type: "text"}, 
    EVENTS({"keyup", {context: this, validateName}})),
  INPUT({type: "button"},
    EVENTS({"click": {context: this, function() { if (validateForm()) submitForm();}}}))
);

ghost avatar Nov 03 '12 09:11 ghost

perhaps the best bet for an approach like this would be to differentiate attributes from properties, so that you could just use on<event-name>?

jed avatar Nov 04 '12 02:11 jed

better yet, support a non-standard on attribute that takes an event name -> event handler map?

jed avatar Nov 04 '12 02:11 jed

I like your second idea better, Would that look something like this?

INPUT({on: {"click": {context: this, handler: doThis, args: [1, "foo", bar]}, 
           {"blur": {handler: doThat}}}
});

ghost avatar Nov 04 '12 04:11 ghost

well, more likely to be just a generic on<event-name> property assignment. the binding you can do outside domo, no?

jed avatar Nov 05 '12 11:11 jed

Oh ok.

ghost avatar Nov 05 '12 18:11 ghost