Dojo supports two programming models, declarative and programmatic. Which one you use depends on what you are doing. In most cases, the declarative model may be easier to use as it is markup but there are times when you will use the programmatic model. You can, of course, intermix the models on the same page as needed.
The best way to show the models is through the use of widgets which can be used either declaratively or programmatically. Although both models are available we will mainly use the declarative model through out this book when both are an option.
The tutorial used the declarative model to create a button on the page using the code below.
<BUTTON widgetId="helloButton" dojoType="Button">Hello World!</BUTTON>
The following declarative formats are equivalent.
<?xml:namespace prefix = dojo /><dojo:widget></dojo:widget> <DIV dojoType="widget"> <DIV class=dojo-widget></DIV></DIV>
We will discuss the declarative model in more detail shortly.
You can also declare widgets programmatically using the dojo.widget.create API as follows. When declaring widgets programmatically, the API returns the widget id which you use to call the appropriate methods.
var myTabPane= dojo.widget.createWidget("TabPane", {id: "myTabPane"}, srcDiv);
Which approach to use is discussed later in the book but for now we want to introduce the idea that you have different options.
Before we get into the declarative model let's look at the widget infrastructure to better understand what methods are available when using widgets in your pages.
In addtion to methods, each class has parameters that are useful. There are two types of parameters listed here.
dojoType instructs Dojo how to process the element when the page is loading. Keep in mind that Dojo manipulates the DOM as it renders the page so you must use the Dojo APIs to access the widget ids. Dojo keeps a reference of all widgets it has created that can be accessed with the dojo.widget.byId function - providing you specify either the widgetId or id attribute in your markup. Also you can use dojoAttachEvent using this method.