Opened 7 years ago

Last modified 2 years ago

#15226 assigned enhancement

domConstruct.create() optional document parameter

Reported by: bill Owned by: dylan
Priority: undecided Milestone: 1.15
Component: HTML Version: 1.7.2
Keywords: Cc:
Blocked By: Blocking:

Description (last modified by bill)

Most of the methods in the dom* modules allow optionally explicitly or implicitly specifying a document. For example:

  • dom.byId(id, doc) looks up "id" in document "doc"
  • domConstruct.place("<div>hi</div>", refNode) creates the <div> in refNode.ownerDocument

However, domConstruct.create() doesn't take an optional document parameter, so you can't easily specify a document for a case like:

domConstruct.create("div", {role: "dialog", innerHTML: "hi"});

If your code has a refNode, the document is implied by the refNode:

domConstruct.create("div", {role: "dialog", innerHTML: "hi"}, refNode);

However, sometimes there's no refNode, in which case you either need to stop using domConstruct.create(), ex:

var div = myDoc.createElement("div");
domAttr.set(div, {role: "dialog", innerHTML: "hi"});

or

var div = domConstruct.toDom("<div role=dialog>hi</div>")[0];

or use the dojo/_base/window withDoc() mechanism, which is a little clunky:

win.withDoc(myDoc, function(){
   domConstruct.create("div", {role: "dialog", innerHTML: "hi"});
});

(I'm also not sure if withDoc() will be around in 2.0.)

So, this is a ticket to consider adding an optional document parameter to the create() method. I'm not sure what the signature would be though, since create() already has a number of optional parameters.

Change History (5)

comment:1 Changed 7 years ago by bill

Description: modified (diff)
Summary: dom.create() document parameterdomConstruct.create() optional document parameter

comment:2 Changed 7 years ago by Eugene Lazutkin

All document-less DOM functions are supposed to work with withDoc(). If not, this is a bug. If they do, we just need to document it better.

Regarding withDoc() in 2.0 --- it is a subject of a separate discussion.

comment:3 Changed 7 years ago by bill

As I wrote in the ticket description, create() does work with withDoc().

However, the other DOM methods (even DOM creation methods like place() and toDom()) can be used directly on any document, without a withDoc() wrapper, so it would be nice if create() could be used that way too.

Otherwise, seems like there's no point to using the create() convenience method at all (when you want to run against a different document), since it's easier to do:

var div = myDoc.createElement("div");
domAttr.set(div, {role: "dialog", innerHTML: "hi"});

than

win.withDoc(myDoc, function(){
   domConstruct.create("div", {role: "dialog", innerHTML: "hi"});
});

comment:4 Changed 3 years ago by dylan

Milestone: tbd1.12
Owner: changed from Eugene Lazutkin to dylan
Status: newassigned

comment:5 Changed 2 years ago by dylan

Milestone: 1.131.15

Ticket planning... move current 1.13 tickets out to 1.15 to make it easier to move tickets into the 1.13 milestone.

Note: See TracTickets for help on using tickets.