views:

405

answers:

5

More of a general question here.

At the moment a lot of the projects I work on utilize server-side views to render the user interface, and spruce it up with some JavaScript here and there. That is all fine and dandy for smaller projects, but lately it seems like the .js files are growing rather large in size, and the stacks upon stacks .live and .bind jQuery calls just don't seem to cut it anymore.

What are good ways to blend JavaScript into the view and, perhaps, the controller of a web application? For the Java-driven websites I found DWR to be quite useful, but a lot of times user initiated events require controller logic, which is starting to become overwhelming and confusing when it's part of the many lone functions included on the pages.

I considered a completely AJAX-driven template engine but that seems to be a bit extreme and will likely be a pain in the butt for anyone to use. Cloning the functionality of the existing backend classes, on the other hand, seems redundant.

What is a good "middle ground" approach used by web apps out there, those that aren't entirely AJAX free nor completely JavaScript driven?

EDIT: Perhaps I'll provide an everyday example of a problem. Say I'd like to provide the user with a modal dialogue confirming or denying something:

"Your picture is uploaded but looks terrible. You need a new 'do." (OK | What?)

Now, in one scenario, that dialogue could pop up as a result of uploading an image with a page refresh, in which case the server-side view will render it. In another scenario, it might appear after uploading the image via AJAX, in which case it'll probably be triggered by JavaScript on the page. In both cases we need to access the dialogue creation code, and I can't so far think up a way to have, say, a Dialog class which would work the same in both cases.

+2  A: 

I'm certainly not an expert in this realm, but in the past have worked with projects utilizing RESTful services which seemed to fit the 'AJAXY' world of web site development nicely. I can't say it'd be ideal for web apps, but worked great for content-rich presentational sites. It seems like it'd fit your need for multi-presentational formats nicely via custom templates. So, the service could call the pictureUpload service using a HTML page template, or it could call the service and request the AJAX component template.

DA
That pertains mostly to models, no? Now what to do about views and controllers? In my example of the alerts, how the alert is rendered and what happens when the user clicks either of the two buttons could be rendered by either the server-side scripts or mashed together by JS at runtime.
mitjak
A: 

I have found myself recently using my server side code (ASP.Net MVC in my case) as a means to provide re-use of my layout components (master files), and small encapsulated bits of UI (partial views), and doing a fair amount of work in javascript. In this particular case I'm still pretty early in my UI work, but with jQuery and jQuery UI I've got a lot of functionality in a very small footprint.

I think one of the challenges to having a mixed solution is figuring out where to put the various bits of logic. After that the rest of it probably goes to figuring out how to re-use as much of your javascript and css code as possible. I still haven't figured out how to manage the various javascript artifacts I end up with (though the Google CDN relieves a lot of that by providing jQuery, jQuery UI, ans the jQuery UI CSS resources).

ckramer
+1  A: 

You can throw all logic at the server, and assume a dumb client that displays whatever the server sends.

There are two scenarios:

  1. Non-Ajax Request
  2. Ajax Request

The only difference between them is that, in the first one you're rendering more content than just the modal dialog. There's no reason why you can't have a Dialog class on the server which spits out the HTML representation of the dialog, and is used for both types of request. Then in the AJAX call, you would simply add the server's response into the DOM.

Like you said, it can be problematic sharing UI creation logic on both client and server side, so it's better to choose one and stick with it. In the above case, all logic is pushed to the server. Read up more about AHAH.

Anurag
+1  A: 

It sounds like Google Web Toolkit might be what you're looking for.

It allows you to write client-side applications in Java and deploy them as JavaScript.

Presumably then you could write the code once in Java and use it in both places, although I've never used GWT myself.

In my own framework that I'm developing, I'm basically forcing developers to write the code twice. Once in the native language, and once in JavaScript. I make them fill in a function which returns the JS, and then it can be called automatically where it's needed. But all the code is contained within one class so at least you don't have the logic spread all over the place, and you can quickly compare if they are functionally equivalent. For things like regular expressions, it can normally be written just once and then passed to JS (I use it to validate once on the client-side, and then again on the server-side).

Mark
+1  A: 

I've been working recently with JavascriptMVC (2.0) for an internal company app. It has its warts, but the overall architecture is good and allows you to create "controller" JS classes. Each controller "owns" a subset of the DOM tree (or if you prefer, a visual part of the page) and responds to events within that zone and uses EJS templates (the "view" part) to alter areas under it. It nicely abstracts what would otherwise be a lot of $(...).bind() and $(...).live() calls into an OOP model.

In my case, our interface is almost 100% JS-driven due to the constraints around the project, but there's no reason you can't mix-n-match.

Now, in one scenario, that dialogue could pop up as a result of uploading an image with a page refresh, in which case the server-side view will render it. In another scenario, it might appear after uploading the image via AJAX, in which case it'll probably be triggered by JavaScript on the page

Here's how I'd do it in a way that works even with Javascript disabled:

  1. Server-side outputs an HTML upload form. The plain-HTML form will submit to another PHP page.
  2. A snippet of Javascript runs when the page finishes loading, looking for that form.
  3. The javascript creates a HairdoUploadController instance, passing in the <form>...</form> to the constructor.
  4. The controller "takes over" the form, using JQuery selectors to alter the styling and to trap the form submitting events.
  5. The controller adds a new div and associates it with a (initially hidden) Jquery-UI Dialog.
  6. When the form is submitted, the controller instead makes an AJAX call, to a slightly different URL than the plain form.
  7. The results of the AJAX call are pushed into the Dialog's div, and the dialog is displayed.
Darien
JSMVC and your approach to the problem sound quite interesting. Thanks for the suggestion!
mitjak
I found this response to be most helpful in solving the problem at hand. On a somewhat related note, if one were not to rely on complete frameworks, this would also be handy:http://code.google.com/p/google-jstemplate/
mitjak