Login Register

Custom Widgets

This page contains information for people writing their own widgets. If you are just using the dijit widgets out of the box, you can skip this section.

Widget Declaration

dojo.defineWidget() has been replaced by plain dojo.declare()

Widget Creation

Widget creation method call sequence:

  1. postMixInProperties()
  2. fillInTemplate() is no longer called.
  3. buildRendering() - swaps out the source HTML with the widget's HTML, typically from a template. _Templated mixin defines this function.
  4. postCreate() is called. Note that unlike 0.4, the children don't yet exist
  5. startup() is called after all the other widgets have been created

Note that those functions' (postCreate, etc) call signatures have changed. No arguments are passed.

To get the source dom Node, just reference this.srcNodeRef

Base Classes

In 0.4 there were three main base classes (Widget, DomWidget, and HtmlWidget), but everyone used HtmlWidget so we can think of that as one class. 0.9 split up things by functionality, into:

  • dijit._Widget
  • dijit._Templated
  • dijit._Container
  • dijit._Contained
  • dijit.layout._LayoutWidget
  • dijit.form._FormWidget

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit._Container");
dojo.require("dijit.layout._LayoutWidget");
dojo.require("dijit.form._FormWidget");

Name changes

Properties
0.40.9Comments
childrendeletedMixin dijit._Container and call getChildren()
parentdeletedMixin dijit._Contained and call getParent()
extraArgsdeleteddeclare all parameters to your widget as attributes in the widget
disableddisabledin _FormWidget
widgetIdid
widgetTypedeclaredClass
nsdeletedNo namespaces anymore; use full path to widget like dijit.form.Button
templateString, templatePathMix in dijit._Templated
templateCssPathdeletedInclude CSS rules directly
toggle, toggleDurationdeletedUse dojo functions (fadeIn, wipeIn) on widget.domNode directly. Some widgets like Dialog still support these attributes (or similar ones) but it's not builtin to every widget.
isContainerdeletedNo longer necessary; all widgets can be containers.

Methods
0.40.9Comments
enable, disablesetDisabled
onResizedlayoutwhen layout is called size is in this._contentBox
createconstructor calls lifecycle code
fillInTemplatedeleteduse postCreate() instead
postInitializedeleteduse postCreate() instead
postCreatewhen postCreate() is called children don't yet exist. If you need to access them, use startup()
destroydestroyRecursivedestroy() doesn't destroy child widgets
getChildrenOfTypedeleteduse dojo.filter(this.getChildren(), function(child){...})
addChild, removeChildmix in dijit._Container
isShowing, toggleShowingdeletedReference this.domNode directly
show, onShow, hide, onHidedeletedSome widgets support these functions but not all widgets. Reference this.domNode directly if necessary.
resizeTo(w, h)resize({w: 10, h: 20})Now you can just change height or width (each argument is optional)
resizeSoondeletedshouldn't be necessary; layout algorithms were changed

Renderers

Removed support for multiple renderers (svg & vml & a11y) for a single widget. If a widget wants to render differently on different platforms, there must be branching code inside the widget, or it needs to call a library that branches based on the browser type (like dojo.gfx or dojo.charting).

Templates

"this." is no longer used within ${} substitution notation. Just say ${foo} to access the foo attribute. dojoRoot,buildScriptBase,dojoModuleUrl are no longer supported, but any JavaScript properties on the widget's 'this' may be referenced with dotted notation.

The attributes dojoOn* are desupported (including dojoOnBuild); also can't use id attribute to affect a dojoAttachPoint.

dojoAttachEvent is case sensitive, so capitalization matters. You will probably have to change

dojoAttachEvent="onClick"
to
dojoAttachEvent="onclick: onClick"
(given that the event name is lowercase, and assuming that the method name is camel case)

Lists within dojoAttachPoint, dojoAttachEvent, waiRole, and waiState are now comma-separated (not separated by semi-colons)

Parent/Child relationships

By default widgets exist as islands, not knowing about their parent widget or children widgets. The exception is the destroyDescendants() function which will also delete all descendant widgets. Some widgets like Tree and SplitContainer know about their children, but those widgets will use the special mixins from Container.js

All custom widgets with children (and templates) must declare dojoAttachPoint=containerNode in the template. It doesn't default to domNode anymore.

Layout Widgets

Layout widgets mixin _LayoutWidget, and support a resize() method.

onResized() removed.

Miscellaneous Utility Functions

Many functions were removed from dojo core but added back into dijit. See HTML Utilities migration guide for details.

Also:

  • dojo.widget.html.layout --> dijit.layout.layoutChildren (but layoutPriority flag was removed).

Typo: dojoModuleUri instead of dojoModuleUrl?

The old method was called dojoModuleUri, not dojoModuleUrl, no?

I found the bit on using template files confusing...

I found the bit on using template files confusing at first... What it means is that you need to add the class dijit._Templated as a "mix-in" class with something like

dojo.declare(
	"my.employer.FancyWidget",
	[dijit.form._FormWidget, dijit._Templated],
	{
...
	}
);

This "mixes in" the properties templateString and templatePath to the JS object of your custom widget. See the file /dijit/_Templated.js for details of the properties available.

srcNodeRef not srcDomNode

This page used to say use this.srcDomNode to reference the original node. It should have been this.srcNodeRef. Its fixed now - sorry for any confusion.