views:

135

answers:

6

Hi,

I know Model-View-Controller well, have known about it for years and used it in terms of server-side development with languages like PHP.

However, I am now working with JavaScript and building a big application with it utilizing SVG, Canvas among other great features modern browsers support. The project is big, so, the architecture behind it must not be fragile.

JavaScript and MVC do not get on like a house on fire, because JavaScript is event-driven by nature. So, are there any architectures or anything else I should definitely learn, understand and implement?

The software will have to deal with data. It already utilizes local storage and web SQL database. I need a Models, right? There is an UI, so I have Views? However, do I have Controllers? What about events? How do I structure everything?

Architecture, architecture, architecture -- that's what I'm interested in. I'm fine with the language of my choice.

A: 

MVC is still the way to go, in my opinion. If you're looking for a good framework to help you achieve that a little less painfully, I would look at JavaScript MVC, it has models, views, controllers, unit testing, jQuery support, etc.

Raul Agrait
A: 

You should learn the Event Based nature of client-side JavaScript and how it blends with MVC based server-side applications.

You should also learn how to properly program inside of the Prototype based inheritance structure of Javascript.

Both of those things will allow you to write you JavaScript so that it meshes with your server-side application framework and is extensible and re-usable.

Justin Niessner
A: 

One thing i have learned over the years of javascript programming is writing UnObtrusive Javascripting which basically means seperating as much as possible structure(HTML) and style(CSS) from Behaviour(JAVASCRIPT).

sushil bharwani
A: 

Althogh not a raw javascript solution, take a look at CoreMVC, the jQuery architechure of MVC.

CorMVC is a jQuery-powered Model-View-Controller (MVC) framework that can aide in the development of single-page, web-based applications. CorMVC stands for client-only-required model-view-controller and is designed to be lowest possible entry point to learning about single-page application architecture. It does not presuppose any server-side technologies, or a web server of any kind, and requires no more than a web browser to get up and running.

Sarfraz
+2  A: 

First, I'm the author of JavaScriptMVC, so I'm extremely biased in a whole variety of ways. First, there are 6ish things you will ever do in a JS application:

  1. Load Scripts
  2. Respond to user events
  3. Update the DOM
  4. Request data from the server
  5. Convert that data into something useful for JavaScript
  6. Organize your front-end business logic

Your choice of architecture might depend on what tools you want / need.

For general architecture, I do think it's important to separate concerns.

I strongly encourage you to find some way of doing dependency management, and client side templates. They will make your life a lot easier.

JavaScriptMVC uses a tiered MVC approach that's based heavily around custom UI events and OpenAjax events.

I build my low-level widgets with $.Controller in a similar way to how you would build jQuery widgets. The big difference is that the widgets produce a non-ui event that top level controllers can listen to. For example, a tabs widget might produce a "tab.activate" event like:

$('.tab').trigger('tab.activated')

Then, my higher order controller might listen to tab.activated events, and a the model to update the tab content like:

".flickr tab.activated" : function(tabEl, ev){
  Flickr.findAll({type : "rainbows"}, function(images){
    tabEl.html("//path/to/view", images );
  }
}

Flickr.findAll essentially does a query for flickr messages, then calls back with a list of images. Wrapping the service/ajax functionality with models makes them a lot more reusable.

You'll notice that in the callback I update the html of the tab element with the rendered content from a view. This probably isn't the 'best' way of doing it, but I wanted a quick example. Better would be passing the tabs controller the rendered output, for it to do what it will with it. That way if your tab wants to fade in content someday, it will be able to and your master controller won't have to know about the tab's implementation.

The most important thing is to break down your app into the smallest pieces you can. Have them individually testable (and flexible), and combine the little parts into bigger parts as you work your way up to your application.

Justin Meyer
`font-end` or `front-end`?
vol7ron
+1  A: 

Take a look at Ext JS. It has a clean architecture that is well-suited towards highly complex javascript applications.

Data handling and server communication is done via stores. Data rendering is done via grids (with in-cell editors), and forms (with a rich set of form controls), which can both talk to the stores. There's also a set of layout classes to abstract away CSS positioning (border layout, box layout, table layout, form layout, ...).

It is however not MVC in the typical sense. The library encourages a programming style that avoids dealing much with HTML and CSS, letting you live (mostly) in pure JavaScript land. You end up thinking in terms of components and data, instead of individual dom elements and style rules. If you don't like that approach, be warned, you won't like this library.

Joeri Sebrechts
I'm very aware of this library, I have been working with it for almost a year now 8 hours a day, 5 days a week. I am not happy with its architecture, it's very poor in many aspects like extensibility and any sort of customization.
rFactor