Re: Element.create(): a proposal for more convenient element creation

On Wed, Aug 3, 2011 at 8:10 AM, Tab Atkins Jr. <jackalmage@gmail.com> wrote:
> On Wed, Aug 3, 2011 at 12:34 AM, Anne van Kesteren <annevk@opera.com> wrote:
>> On Tue, 02 Aug 2011 20:31:04 +0200, Tab Atkins Jr. <jackalmage@gmail.com>
>> wrote:
>>> On Tue, Aug 2, 2011 at 11:26 AM, Glenn Maynard <glenn@zewt.org> wrote:
>>>> On Tue, Aug 2, 2011 at 2:18 PM, Tab Atkins Jr. <jackalmage@gmail.com>
>>>> wrote:
>>>>> MooTools is basically identical to Prototype, except that you can
>>>>> additionally set listeners on the element during creation by using a
>>>>> magical "events" property in the attribute bag, which takes an object
>>>>> of event names and functions.  This would be nice to look into adding.
>>>>
>>>> Is this much better than just saying eg. Element.create("a", {href:
>>>> "http://link", onclick: function(e) { ... } }, "link"}?
>>>
>>> Hmm, is everything exposed as on* attributes now?  If so, then yeah,
>>> just do that; no need to mess around with a magic property in the
>>> attributes bag.
>>
>> This would still be magical as it is setting an IDL attribute rather than a
>> content attribute.
>
> Hmm.  onclick is a content attribute, no?  Or do you just mean that
> assigning a function directly (rather than a string of code) is
> something that can only be done via an IDL attribute?
>
> If so, then good point, but I also expect that this wouldn't be very confusing.

It would seem very inconsistent if some attributes are set using
elem.setAttribute and others using elem.foo=bar. Would you make the
distinction based on that the attribute name starts with "on"?

One possible solution would be to be able to specify event handler
attributes in a second object, so something like:

Element.create("a", { href: "..." }, { onclick: function(e) { ... } },
"link", anotherChild);

On the other hand, it might be ok to say that all attributes whose
name start with "on" and whose value is a Function object is set using
the IDL property rather than setAttribute.

/ Jonas

Received on Thursday, 4 August 2011 21:52:49 UTC