views:

321

answers:

7

How should a complex single-page JS web application be structured on the client-side? Specifically I'm curious about how to cleanly structure the application in terms of its model objects, UI components, any controllers, and objects handling server persistence.

MVC seemed like a fit at first. But with UI components nested at various depths (each with their own way of acting on/reacting to model data, and each generating events which they themselves may or may not handle directly), it doesn't seem like MVC can be cleanly applied. (But please correct me if that's not the case.)

--

(This question resulted in two suggestions of using ajax, which is obviously needed for anything other than the most trivial one-page app.)

A: 

I would go with jQuery MVC

Reigel
A: 

Check out http://bennadel.com/projects/cormvc-jquery-framework.htm Ben is pretty sharp and if you dig around on his blog he has some nice posts about how CorMVC is put together and why.

patrickmcgraw
A: 

You can use javascript MVC framework http://javascriptmvc.com/

bjornd
+1  A: 

The way I build apps:

  • ExtJS framework, single page app, every component defined in a separate JS file, loaded on-demand
  • Every component contacts its own dedicated web service (sometimes more than one), fetching data into ExtJS stores or special-purpose data structures
  • The rendering uses standard ExtJS components, so I can bind stores to grids, load forms from records, ...

Just choose a javascript framework, and follow its best practices. My favorites are ExtJS and GWT, but YMMV.

Do NOT roll your own solution for this. The effort required to duplicate what modern javascript frameworks do is too big. It is always faster to adapt something existing than to build it all from scratch.

Joeri Sebrechts
+2  A: 

MVC architecture of PureMVC/JS is the most elegant IMO. I learned a lot from it. I also found Scalable JavaScript Application Architecture by Nicholas Zakas helpful in researching client side architecture options.

Two other tips 1) I've found view, focus, and input management are areas that need special attention in single page web apps 2) I also found it helpful to abstract away the JS library, leaving door open to change mind on what you use, or mix & match should the need arise.

Dean Burge
A: 

hi,

i had the same question sometime back and managed to write a fully functioning framework with widget and popup support based on jquery. with implementation of MVC support and lazy loading too. i still couldn't blog it. i will update this post as soon as i do that, or feel free to contact me if its urgent.

-- Praveen Gunasekara

Praveen
+1  A: 

The web application that I am currently working on uses JQuery and I would not recommend it for any large single page web application. Most frameworks i.e. Dojo, yahoo, google and others use namespaces in their libraries but JQuery does not and this is a significant drawback.

If your web site is intended to be small then JQuery would be ok but if you intended to build a large site then I would recommend looking at all the Javascript frameworks available and deciding which one most meets your needs.

And I would recommend applying the MVC pattern to your javascript/html and probably most of your object model for the javascript could be done as the json that you actually return from the server through ajax and the javascirpt uses the json to render html.

I would recommend reading the book Ajax in action as it covers most of the stuff you will need to know.

ealgestorm