![]() |
Site Archive (Complete) | |||
|
ABOUT US |
CONTACT |
ADVERTISE |
SUBSCRIBE |
SOURCE CODE |
CURRENT PRINT ISSUE |
NEWSLETTERS
|
RESOURCES
|
BLOGS
|
PODCASTS
|
CAREERS
|
||||
January 01, 2002
The DOM in Version 5 Browsers
June 18, 1999 Issue > Browsers
JavaScript for the MVC exampleBy Mitch Gould
Let's look at the JavaScript that runs our example. In studying mvctable.js, a veteran DHTML coder will find some surprises due to its use of the new DOM.
For the sake of clarity, mvctable.js is divided into four parts:
Section A is simply expected in a script of any size. Here, it sets up the string arrays that contain the Alice quotations and their sources.
The code in the B section provides a way to implement a model-view-controller design pattern using the Document Object Model. This section acts as the controller, initializing the model and view, refreshing the model and view with updated values, and destroying the view upon request.
Section C provides support routines that encapsulate lower-level details of access to document objects. It's a bit disappointing that the DOM doesn't provide standardized access to the document at this level of encapsulation, but once these utility methods are written, they can be reused endlessly. These methods are responsible for navigating the document tree to touch the desired objects, notably in this case, text content that must be replaced.
The D section is required to start the controller as soon as the browser finishes loading the page.
With this background, we're ready to examine the code example. As the page is loading, the browser processes the data initialization section, executing the constructor
model = new Array()
to create an empty model, and creating two more arrays:
Initializing the controller
Once the page has finished loading, the controller starts.
The controller transfers the quotes to the model, and creates a new table in the document to act as a view, as shown in Listing 3.
Listing 3. The
To understand the functions called by
JavaScript for the MVC example
|
|
|||||||||||||||||||||
|
|