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

巧了我就是萌 提交于 2019-12-05 00:58:20

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!