- The Book of Dojo
- The Dojo Book, 0.4
- Part 1: "Introduction"
- Part 2: "Out of the Box" Dojo
- Part 3: "The Dojo Programming Model"
- Part 4: "More on Widgets"
- Part 5: "Connecting the pieces"
- Part 6: "Customizing Dojo Builds for Better Performance"
- Part 7: "Utilities"
- Part 8: "Internationalization and Accessiblity"
- Part 9: "Dojo Community"
- Part 10: "Fresh From The Shed" Dojo
- BookWriting
- Glossary
Declarative vs Programmatic model
Submitted by Carla on Mon, 01/29/2007 - 22:27.
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.
Infrastructure
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.
- Lifecycle methods are called by the infrastructure when a widget is created. They are not meant to be called by the user.
- Internal methods are called by the widget class code for supporting functionality and are "private" and not meant to be called by the application developer. Unfortunately there is no easy way to identify the lifecycle or internal methods. We are working on conventions that will help identify these methods in future releases.
- "on" methods begin with the string "on" and are available to application developers. These are called by the widget infrastructure based on an action or event. Application developers can provide application specific code for these methods and those that application specific code will be called automatically when the related event or action is triggered.
- Developer methods are the rest of the methods in the widget. They are provided for use in the programmatic model.
In addtion to methods, each class has parameters that are useful. There are two types of parameters listed here.
- init parms are set at initialization time and then are readonly
- rest of the list should be in the API doc
Declarative model
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.
