Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am working on a web app that displays some data and uses javascript.

Right now, we are serving up parts of our js (to display certain types of information, ie "Render a View") via our server which dynamically writes the java script with the needed data embedded in it using JSPs.

Three main points/ requirements we need to consider are:

  • We need to be able to easily edit our javascript. (spread through multiple files right now with global variables. can't edit anything with out mabey(no testing framework) breaking everything.

  • We want to be able to make calls on the client side to render a view without knowing specifics about the View (in case we add/ change a View).

  • We need to test our javascript (for obvious reasons)

Currently we have our js very spread around and in large files which makes it hard to make edits. I've been looking into the module pattern to address this concern, but many of the posts I've been reading have been:

(a) Older (most recent from 2010)

(b) Had some disagreement about how to make modules that are also testable

Any ideas about ways we should change/ restructure our application to accomplish all three goals? do these goals have too much inherent conflict, or is there hope? Also, any helpful blog posts are appreciated.

share|improve this question
6  
"our server which dynamically writes the java script " This is your problem right there, why would you dynamically generate code? –  Benjamin Gruenbaum Jan 22 '13 at 21:25
    
@BenjaminGruenbaum: I assume that is being done to provide the "render a view without knowing specifics about the view" capability. –  Robert Harvey Jan 22 '13 at 21:25
    
Our js is spread through multiple files (including the files that do the generation) there are also global variables used in every corner of every files (yes, includeing the generated files. its impossible to edit anything with out breaking everything right now. –  hhamilton Jan 22 '13 at 21:28
2  
@BenjaminGruenbaum dynamically writes the java script with the needed data embedded in it - this is more commonly known as a "template" ... –  Izkata Jan 22 '13 at 21:31
4  
@Izkata templating code is a horrible idea, code is code, you can just pass it the required data, dynamically generated code is not maintainable and testable. OP, the module pattern is a way to organize your code, you can read about organizing javascript code in this excellent book by addy osmani addyosmani.com/resources/essentialjsdesignpatterns/book My first thought for you is to get rid of all the globals, stop writing code dynamically, and organize the code into modules using require.js or such. –  Benjamin Gruenbaum Jan 22 '13 at 21:34

1 Answer 1

up vote 1 down vote accepted

So as a follow up, What worked in this instance was doing almost exactly as Benjamin Gruenbaum said. "templating" the code, or having one piece of code write another one was bad for many reasons, including performance (no caching of js), maintainability and test-ability. I combed apart the system into a server part that encoded the needed data into json and then sent it to our js on the client side, as well as started to organize the code into modules, which helped tidy things up a lot so far, and I foresee leading to better (again) maintainability and test-ability. I can also endorse http://addyosmani.com/resources/essentialjsdesignpatterns/book/ as a great, current resource on how to write bigger pieces of javascript in a well organized and generally right way.

share|improve this answer
    
Don't forget to mark your answer as accepted (in this case to handle your own accept rate, and to make this question not show up as unanswered). Also, you might want to check out Backbone.js and Require.js and how they structure their code. Even if you don't use them, they might offer some insights into how to make your own JS more maintainable. –  Shauna Feb 27 '13 at 14:50
    
I have now checked out both those frameworks (and wandered into angular.js which looks very useful to me). Thanks for the reminder. –  hhamilton Feb 27 '13 at 19:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.