Changing page without reloading browser

JavaScript March 6th, 2009 @ 13:03

I wanted to investigate if we can build web applications that doesn’t require reloading of pages in the browser, according to the principles of  Life above the Service Tier.

Evaluation

I started by reading the “Life Above the Service Tier” document, which was a description how it works today (and its flaws) and what the proposed solution is.
Also, I browsed the web for more articles, blog posts and comments on the issue.

Summary

The ideology (called SOFEA: Service-Oriented Front-End Architecture) is basically to put more rendering logic on the client side and only use the server side to transport data back and forth.

The principles of SOFEA could be implemented with help from AJAX. The controller resides on the client-side and is responsible for loading (XML) data and rendering the application using the received data, without actually reloading the page in the browser.
This means that JavaScript would be used instead of HTML/JSP to render the server data. Also, the server-side is much thinner and not really in need of any web framework (as all it does is receive messages and return XML data).

Conclusion

The principles of SOFEA seem very far away from today. There are several flaws with implementing in the suggested manor:

  • The web clients (browsers) of today are not good enough to handle could have problems handling as much logic as a large web site would require (given that old IE versions have so many users, ca 70-85%)
  • The client code (JavaScript) and architecture is not really mature/good enough to handle this much logic, given that it needs to be bullet-proof, easy to test etc (EDIT: unless using GWT or similar – it’s at least difficult to manage)
  • Developing applications would take much more time (hence be more expensive) as more manual labour is required to render content using JavaScript instead of letting the JSP engine do it with JSP/HTML/PHP/etc

Everything points to NOT implementing it in such a manor. At least not at this time, partly because the browser/client market isn’t ready for it.

Is there an other way?

Yes, there are other solutions to the main issue; changing pages without actually reloading the container (browser).

One suggested solution could be an extension to our model in Taikai, where AJAX can be used in combination with both XML, HTML/JSP and JSON, hence giving us the possibility to smoothly serve a full JSP page using AJAX.

A brief description of how it could be solved:

  • Make a JavaScript “controller” handle all URL requests to the server, in order to bind the answer to a certain action.
  • Set up “View XML files” for all views that are to be loaded with AJAX. The view could look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xmlRoot>
<title>Document title</title>
<styles>
<src>/css/foo.css</src>
<src>/css/bar.css</src>
</styles>
<scripts>
<src>/js/foo.js</src>
<src>/js/bar.js</src>
</scripts>
<content id="targetContainerId">
<![CDATA[<jsp:include file="foo.jsp" />]]>
</content>
</xmlRoot>
  • When returning the response from the server, the returning page would be the pre-defined XML (as per above).
  • The controller parses the contents of the returned XML file and performs its magic:
  1. Changing the document title
  2. Loading new styles and scripts (and possibly unloading old ones to keep the page slim)
  3. Updating the content element (with id=”targetContainerId”) with the provided content, which has been rendered by the JSP engine and therefore taking advantage of its power (i.e. JSTL, HTML tags etc etc).
  4. Updating the browser history using hash (#) technique, allowing for back/forward movement while remaining in the same page container

The main advantages with such a solution would be:

  • Allowing developers to create content using known and powerful formats, such as HTML and JSP
  1. Very important in respect to time efficiency
  2. Still allows for easier debugging and development
  3. No need to put too much logic on the JavaScript side, i.e. creating elements etc (although it could still be done in some components, if needed/wanted)
  • Still providing the user with the quicker experience of a site without (or with few) full page reloads

Remaining questions:

  • Would there be any risk of memory leaks, especially in older Internet Explorer browsers? And is there an easy way of eliminating these?
  • Is the support for “hash history technique” good enough in all major browsers?

References

Testing the PureMVC framework

Flash & Flex March 6th, 2009 @ 11:03

Objective

Testing the PureMVC framework.

Evaluation

I decided to test using the PureMVC framework in a small application in order to find its pros and cons.

As I had tested setting up the framework once before (in another project), it wasn’t really a big deal installing it and creating my first classes. Also, after having spent a few hours reading the PureMVC documentation and best practices, I already had knowledge on how the system worked.

Result

After completing the application, I’ve found both pros and cons with the implementation of PureMVC.
(Please note that I haven’t taken other pros/cons to consideration here, such as how often the framework is updated, community size etc.)

Pros

  • A clear structure for Models, Views and Controllers – the roles of each part is quite easy to understand and make use of.
  • Good use of Controller part of MVC, which usually is difficult to put in the AS equation. In PureMVC, the Controller is a “Command” instructing the application on what to do next (i.e. to register Proxies/Mediators).
  • Good and clear separation of “true” View and Model via Proxy and Mediator – makes it easier to let the View actually “be a View” and Model “be a Model”. The View component (i.e. an actual MovieClip) is manipulated through the Mediator and the Model (i.e. a data object, such as a Player) is manipulated through a Proxy. This ensures that no logic is misplaced in the View component.
  • Nice being able to access Proxies, Mediators etc through the ApplicationFacade from wherever. Allows for Commands to be quite complicated if needed.

Cons

  • All Mediators, Proxies and Commands are registered using a unique key. Hence, there are some difficulties keeping track of Mediators/Views (“M-V”) that are unique for the Proxy’s data object, i.e. the M-V of a certain “Player” with a certain “id”. Perhaps M-V:s should only be implemented for functionality that will only require a single instance of its type?
  • Could be troublesome to keep track of the dependencies etc in the notification system, although the references can quite easily be searched using an IDE. This is probably an issue no matter which system being used.

Other observations (not necessarily to be considered as either pros or cons)’

  • The PureMVC “pattern” needs to be implemented all the way, meaning that the view (component) cannot contain any functionality to affect the world outside of it. A mediator’s “view component” must be completely isolated.
  • Setting up a new class requires some manual labor (as some classes needs to be extended/interfaces implemented), i.e. specifying which events to listen to and how it affects the class

What do I need to know to get started?

It takes a little effort to learn the system and understand what its parts do; i.e. what a Mediator is and how it correlates to the actual View. But once you understand the system, it’s really easy to create new classes for your application.

A few vital things that need to be understood before using PureMVC (I’ll try to sum it up, but the official docs are recommended):

  • What are Notifications? All Mediators, Proxies and Commands listen to notifications specified by the user. If a certain Mediator should display a message at a certain time, it will listen for a notification of that type.
  • What the Mediator is and why it’s being used: it’s basically a layer to the actual View (MovieClip), handling all functionality related to the other parts of the application (i.e. outside of the View)
  • What the Proxy is and why it’s being used: works in the same way as a Mediator, but in a more simpler way. Manipulates the “true” model object, which could be a domain object such as a “Player” or “Table” in a card game application.
  • Where is the Controller? The controller is implemented as a Command, meaning that it’s invoked when a certain notification is sent. Once invoked, it has no function until the notification is sent again.

Conclusion

In my point of view, PureMVC is a good framework to use for MVC applications. It can get a bit complex with boilerplate code, though.

References