W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2011

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

From: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Tue, 2 Aug 2011 11:18:58 -0700
Message-ID: <CAAWBYDD_VfTNvg_d5ux1_JKFDGX6YWoj47-msbxV06pBnhOk_w@mail.gmail.com>
To: Aryeh Gregor <ayg@aryeh.name>
Cc: Maciej Stachowiak <mjs@apple.com>, WebApps WG <public-webapps@w3.org>
On Tue, Aug 2, 2011 at 9:48 AM, Aryeh Gregor <ayg@aryeh.name> wrote:
> On Mon, Aug 1, 2011 at 9:33 PM, Maciej Stachowiak <mjs@apple.com> wrote:
>> In an IRC discussion with Ian Hickson and Tab Atkins, we can up with the
>> following idea for convenient element creation:
>> Element.create(tagName, attributeMap, children…)
>>    Creates an element with the specified tag, attributes, and children.
>
> How does this compare to popular JS helper libraries like jQuery?  It
> would be useful to know what convenience APIs authors are using now
> before introducing our own.

jQuery's element creation is basically driven by innerHTML.  That is,
to create an element, you just make a call like "$('<p class=foo>')".
I doubt that's a pattern we actually want to copy, as it's kinda
dirty, and inconvenient in some cases.  (For example, to apply a bag
of properties as attributes, you have to first create the element,
then call attr() on it.  You can't pass the attrs as an initial arg
without string-building.)

Prototype's element creation is almost identical to what is proposed
here, except it uses something that looks like a constructor.  You
create an element with "new Element('p',{class:'foo'})".  You can't
set children as part of the initial call; they have to be appended in
later calls.

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.

Dojo uses a factory method fairly similar to what's proposed (with the
same name, even - Dojo.create()).  Its first two arguments are the
tagname and an attribute bag, same as the proposal.  Its next two
arguments are used to set a parent node and offset within that parent
node, for automatic DOM insertion after creation.  I don't think it's
valuable to have this in the constructor, though the facilities that
the libraries offer for easier DOM insertion should definitely be
looked at separately.

I think those are the major libraries to pay attention to.  It looks
like jQuery's model is probably not something we want to emulate,
while the other three libraries are almost identical to this proposal.
 The one thing I suggest looking into is the ability to set listeners
on an element during creation, like MooTools allows.

~TJ
Received on Tuesday, 2 August 2011 18:19:47 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:46 GMT