Online Book Reader

Home Category

AJAX In Action [82]

By Root 4143 0

this.value=value;

var valDiv=this.renderSimple();

var td=this.valTd;

td.replaceChild(valDiv,td.firstChild);

this.objectViewer

.notifyChange(this); g

Notify observers

}

}

Editing a property involves several steps. First, we want to assign an onclick handler to the DOM element displaying the value, if the field is editable b. We also assign a specific CSS classname to editable fields, which will make them change color when the mouse hovers over them. We need the user to be able to realize that she can edit the field, after all.

editSimpleProperty() c is a simple event handler that retrieves the reference to the PropertyViewer from the clicked DOM node and calls the edit() method. This way of connecting the View and Controller should be familiar from section 4.3.1. We check that the property type is correct and then replace the read-only label with an equivalent-sized HTML form text input, containing the value d. We also attach an onblur handler to this text area e, which replaces the editable area with a read-only label f and updates the domain model. We can manipulate the domain model in this way, but in general, we would often like to take some other action when the model is updated. The notifyChange() Licensed to jonathan zheng

156

CHAPTER 4

The page as an application

method of the ObjectViewer g, invoked in the commitEdit() function, comes into play here. Listing 4.16 shows this function in full.

Listing 4.16 ObjectViewer.notifyChange()

objviewer.ObjectViewer.prototype

.notifyChange=function(propViewer){

if (this.onchangeRouter){

this.onchangeRouter.notify(propViewer);

}

if (this.parentObjViewer){

this.parentObjViewer.notifyChange(propViewer);

}

}

objviewer.ObjectViewer.prototype

.addChangeListener=function(lsnr){

if (!this.onchangeRouter){

this.onchangeRouter=new jsEvent.EventRouter(this,"onchange");

}

this.onchangeRouter.addListener(lsnr);

}

objviewer.ObjectViewer.prototype

.removeChangeListener=function(lsnr){

if (this.onchangeRouter){

this.onchangeRouter.removeListener(lsnr);

}

}

The problem we are facing—notifying arbitrary processes of a change in our domain model—is ideally solved by the Observer pattern and the EventRouter object that we defined in section 4.3.3. We could attach an EventRouter to the onblur event of the editable fields, but a complex model may contain many of these, and our code shouldn’t have visibility of such fine details in the ObjectViewer implementation. Instead, we define our own event type on the ObjectViewer itself, an onchange event, and attach an EventRouter to that. Because our ObjectViewers are arranged in a tree structure when drilling down on object and array properties, we pass onchange events to the parent, recursively. Thus, in general, we can attach listeners to the root ObjectViewer, the one that we create in our application code, and changes to model properties several layers down the object graph will propagate back up to us. A simple example of an event handler would be to write a message to the browser status bar. The top-level object in a model of planets is the solar system, so we can write

Licensed to jonathan zheng

Summary

157

var topview=new objviewer.ObjectViewer

(planets.solarSystem,mainDiv);

topview.addChangeListener(testListener);

where testListener is an event-handler function that looks like this: function testListener(propviewer){

window.status=propviewer.name+" ["+propviewer.type+"] =

"+propviewer.value;

}

Of course, in reality, we would want to do more exciting things when the domain model changes, such as contacting the server. In the next chapter, we’ll look at ways of contacting the server and put our ObjectViewer to further use. 4.6 Summary

The Model-View-Controller pattern is an architectural pattern that has been applied to the server code of classic web applications. We showed how to reuse this pattern on the server in an Ajax application, in order to generate data feeds for the client. We also applied the pattern to the design of the client itself and developed

Return Main Page Previous Page Next Page

®Online Book Reader