Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between registry.byId and dom.byId in dojo? What is the advantage of using registry.byId?

Tags:

dojo

What is the difference between registry.byId and dom.byId in dojo? What is the advantage of using registry.byId?

In the code below I'm using dijit/registry and dojo/dom for both my textbox (#myTextBox3) and my textbox node (#textNode3). Only two of them are providing results.

require(["dojo/parser", "dojo/dom", "dijit/registry", "dijit/form/TextBox", "dojo/domReady!"],
    function(parser, dom, registry) {

    parser.parse();

    // Locate the JS object.
    var dibiWidget = registry.byId("myTextBox3");
    var dobiWidget = dom.byId("myTextBox3");
    var dibiDOM = registry.byId("textNode3");
    var dobiDOM = dom.byId("textNode3");


    dom.byId("textNode3").innerHTML = "registry.byId for widget id returned: " + dibiWidget + "<br>" +
        "dom.byId for widget id returned: " + dobiWidget + "<br>" +
        "registry.byId for dom id returned: " + dibiDOM + "<br>" +
        "dom.byId for dom id returned: " + dobiDOM + "<br>";
});
like image 864
user3587145 Avatar asked Apr 29 '14 22:04

user3587145


2 Answers

These modules have a different usage. So there is no advantage of using registry.byId() (or dom.byId()) because they differ in use case.

dijit/registry::byId()

The dijit/registry module main use is retrieving widget instances. Quoting the reference guide:

dijit/registry stores a collection of all the dijit widgets within a page. It is commonly used to retrieve a reference to a widget from a related piece of data

dojo/dom::byId()

The dojo/dom module on the other hand is just a module to access DOM nodes. Quoting the information of byId() on the reference guide:

This is a simple alias to document.getElementById, which not only is shorter to write, but fortunately works in all browsers. It turns a domNode reference to some Node byId, or the same node reference if passed a domNode.

What does it mean?

The registry.byId() function will return an instance of your widget. This contains setters/getters and other stuff from the widget. This module should only be used to retrieve widgets, you cannot get a DOM node with it.

The dom.byId() function on the other hand will return the matching DOM node. You can only use it to retrieve a DOM node. A widget obviously also contains DOM nodes, but you should never directly access the DOM nodes of a widget because they're part of the internal structure of the widget (and may change).

When accessing a widget, always use registry.byId(). It provides APIs to access most DOM properties anyways.


Your code

So, your code demonstrates the 4 possibilities here. Assuming #myTextBox3 is a widget (for example of type dijit/form/TextBox) and #textNode3 is a DOM node, the following will happen:

  • dibiWidget will work because #myTextBox3 is a widget. It will return a reference to that widget
  • dobiWidget will probably work, because there is a DOM node behind every widget with the same ID (not required though). However, like I just explained, it's not recommended to use it.
  • dibiDom will not work because there is no widget with ID #textNode3. This is just a simple DOM node.
  • dobiDom will return a reference to the DOM node and will work.

I also made a small JSFiddle to demonstrate this.

like image 198
g00glen00b Avatar answered Nov 10 '22 12:11

g00glen00b


dom.byId() is just short for document.getElementById(...). It returns a reference to the dom node.

registry.byId(...) returns a reference to a dojo widget that is contained in dojo's registry.

For example if you have <div id='myDiv'></div>, You can't call registry.byId('myDiv') here because it isn't a dojo widget (thus isn't in the dojo registry). You can call dom.byId('myDiv').

Now if you had <div id='myDiv' data-dojo-type='dijit/layout/ContentPane'></div>, You can call both dom.byId('myDiv') and registry.byId('myDiv'). One gets you the dom node and the other gets you the dojo widget. Both will have different methods available to them but I generally favor registry.byId(...) if there is an overlap.

There isn't an advantage to using one or the other because they are both different things and used for different things.

https://dojotoolkit.org/reference-guide/1.9/dijit/registry.html

http://dojotoolkit.org/reference-guide/1.9/dojo/dom.html

like image 26
James Sampica Avatar answered Nov 10 '22 13:11

James Sampica