<p>How is your javaScript code organized? Does it follow patterns like MVC, or something else? </p>

<p>I've been working on a side project for some time now, and the further I get, the more my webpage has turned into a full-featured application. Right now, I'm sticking with <a href="http://jquery.com" rel="noreferrer">jQuery</a>, however, the logic on the page is growing to a point where some organization, or dare I say it, "architecture" is needed. My first approach is "MVC-ish":</p>

<ul>
<li>The 'model' is a JSON tree that gets extended with helpers</li>
<li>The view is the DOM plus classes that tweak it</li>
<li>The controller is the object where I connect events handling and kick off view or model manipulation</li>
</ul>

<p>I'm very interested, however, in how other people have built more substantial javaScript apps. I'm not interested in GWT, or other server-oriented approaches... just in the approach of "javaScript + &lt;generic web service-y thingy here&gt;"</p>

<p>Note: earlier I said javaScript "is not really OO, not really functional". This, I think, distracted everyone. Let's put it this way, because javaScript is unique in many ways, and I'm coming from a strongly-typed background, I don't want to force paradigms I know but were developed in very different languages.</p>

[{'answer_id': 32594, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 4, 'selected': True, 'text': "<p>..but Javascript has many facets that <strong>are</strong> OO.</p>\n\n<p>Consider this:</p>\n\n<pre><code>var Vehicle = jQuery.Class.create({ \n   init: function(name) { this.name = name; } \n});\n\nvar Car = Vehicle.extend({ \n   fillGas: function(){ \n      this.gas = 100; \n   } \n});\n</code></pre>\n\n<p>I've used this technique to create page-level javascript classes that have their own state, this helps keep it contained (and I often identify areas that I can reuse and put into other classes).</p>\n\n<p>This is also especially useful when you have components/server controls that have their own script to execute, but when you might have multiple instances on the same page.  This keeps the state separate.</p>\n"}, {'answer_id': 32602, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 1, 'selected': False, 'text': '<p>Not 100% sure what you mean here, but I will say that after doing ASP.NET for the last 6 years, my web pages are now mostly driven by JavaScript once the basic page rendering is done by the server. I use JSON for everything (have been for about 3 years now) and use <a href="http://www.mochikit.com" rel="nofollow noreferrer"><strong>MochiKit</strong></a> for my client-side needs.</p>\n\n<p>By the way, JavaScript <em>is</em> OO, but since it uses prototypical inheritance, people don\'t give it credit in that way. I would also argue that it is functional as well, it all depends on how you write it. If you are really interested in functional programming styles, check out <a href="http://www.mochikit.com" rel="nofollow noreferrer"><strong>MochiKit</strong></a> - you may like it; it leans quite a bit towards the functional programming side of JavaScript.</p>\n'}, {'answer_id': 35177, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 2, 'selected': False, 'text': "<p>MochiKit is great -- and was my first love, so-to-speak, as far as js libraries go.  But I found that while MochiKit has very expressive syntax, it didn't feel nearly as comfortable to me as Prototype/Scriptaculous or jQuery did for me.</p>\n\n<p>I think if you know or like python, then MochiKit is a good tool for you.</p>\n"}, {'answer_id': 62770, 'author': 'Tristan Juricek', 'author_id': 3436, 'author_profile': 'https://Stackoverflow.com/users/3436', 'pm_score': 2, 'selected': False, 'text': '<p>Thank you all kindly for your answers. After some time, I\'d like to post what I\'ve learned so far.</p>\n\n<p>So far, I see a very large difference the approach using something like <a href="http://extjs.com" rel="nofollow noreferrer">Ext</a>, and others like  <a href="http://ui.jquery.com" rel="nofollow noreferrer">JQuery UI</a>, <a href="http://script.aculo.us" rel="nofollow noreferrer">Scriptaculous</a>, <a href="http://mochikit.org" rel="nofollow noreferrer">MochiKit</a>, etc. </p>\n\n<p>With Ext, the HTML is just a single placeholder - UI goes here. From then on, <b>everything</b> is described in JavaScript. DOM interaction is minimized under another (perhaps stronger) API layer.</p>\n\n<p>With the other kits, I find myself starting by doing a bit of HTML design, and then extending the DOM directly with snazzy effects, or just replacing the form input here, an addition there.</p>\n\n<p>The major differences start to happen as I need to deal with event handling, etc. As modules need to "talk" to each other, I find myself needing to step away from the DOM, abstracting it away in pieces.</p>\n\n<p>I note that many of these libraries also include some interesting modularization techniques as well. A very clear description is contributed on the Ext website, which includes <a href="http://extjs.com/learn/Manual:Basic_Application_Design" rel="nofollow noreferrer">a fancy way to "protect" your code with modules</a>. </p>\n\n<p>A new player I haven completely evaluated is <a href="http://sproutcore.com" rel="nofollow noreferrer">Sproutcore</a>. It seems like Ext in approach, where the DOM is hidden, and you mostly want to deal with the project\'s API.</p>\n'}, {'answer_id': 62961, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Tristan, you will find that when you try to architecture JavaScript as an MVC application it tends to come up short in one area -- the model. The most difficult area to deal with is the model because the data does not persist throughout the application, and by nature the models seem to change on the client-side pretty consistently. You could standardize how you pass and receive data from the server, but then at that point the model does not really belong to JavaScript -- it belongs to your server-side application.</p>\n\n<p>I did see one attempt awhile back where someone created a framework for modeling data in JavaScript, much like the way SQLite belongs to the application. It was like Model.select( "Product" ) and Model.update( "Product", "Some data..." ). It was basically an object notation that held a bunch of data to manage the state of the current page. However, the minute you refresh, all that data is lost. I\'m probably off on the syntax, but you get the point.</p>\n\n<p>If you are using jQuery, then Ben\'s approach is really the best. Extend the jQuery object with your functions and properties, and then compartmentalize your "controllers". I usually do this by putting them into separate source files, and loading them on a section-by-section basis. For instance, if it were an e-commerce site, I might have a JS file full of controllers that handle functionality for the checkout process. This tends to keep things lightweight and easy to manage.</p>\n'}, {'answer_id': 190378, 'author': 'Tahir Akhtar', 'author_id': 18027, 'author_profile': 'https://Stackoverflow.com/users/18027', 'pm_score': 2, 'selected': False, 'text': '<p>Just a quick clarification. </p>\n\n<p>It is perfectly feasible to write GWT apps that are not server-oriented. I am assuming that from Server-Oriented you mean GWT RPC that needs java based back-end. </p>\n\n<p>I have written GWT apps that are very "MVC-ish" on the client side alone. </p>\n\n<ul>\n<li>The model was an object graph. Although you code in Java, at runtime the objects are in javascript with no need of any JVM in either client or server-side. GWT also supports JSON with complete parsing and manipulation support. You can connect to JSON webservices easily, see <a href="http://code.google.com/support/bin/answer.py?answer=65632&amp;topic=11368" rel="nofollow noreferrer">2</a> for a JSON mashup example. </li>\n<li>View was composed of standard GWT widgets (plus some of our own composite widgets)</li>\n<li>Controller layer was neatly separated from View via Observer pattern.</li>\n</ul>\n\n<p>If your "strongly-typed" background is with Java or similar language, I think you should seriously consider GWT for large projects. For small projects I usually prefer jQuery.  Upcoming <a href="http://code.google.com/p/gwtquery/" rel="nofollow noreferrer">GWTQuery</a> that works with GWT 1.5 may change that though not in near future because of abundance of plugins for jQuery.</p>\n'}, {'answer_id': 1316984, 'author': 'Justin Meyer', 'author_id': 161238, 'author_profile': 'https://Stackoverflow.com/users/161238', 'pm_score': 2, 'selected': False, 'text': "<p>JavaScriptMVC is a great choice for organizing and developing a large scale JS application.</p>\n\n<p>The architecture design very well thought out.  There are 4 things you will ever do with JavaScript:</p>\n\n<ol>\n<li>Respond to an event </li>\n<li>Request Data / Manipulate Services (Ajax) </li>\n<li>Add domain specific information to the ajax response. </li>\n<li>Update the DOM</li>\n</ol>\n\n<p>JMVC splits these into the Model, View, Controller pattern.</p>\n\n<p>First, and probably the most important advantage, is the Controller.  Controllers use event delegation, so instead of attaching events, you simply create rules for your page.  They also use the name of the Controller to limit the scope of what the controller works on.  This makes your code deterministic, meaning if you see an event happen in a '#todos' element you know there has to be a todos controller.</p>\n\n<pre><code>$.Controller.extend('TodosController',{\n   'click' : function(el, ev){ ... },\n   '.delete mouseover': function(el, ev){ ...}\n   '.drag draginit' : function(el, ev, drag){ ...}\n})\n</code></pre>\n\n<p>Next comes the model.  JMVC provides a powerful Class and basic model that lets you quickly organize Ajax functionality (#2) and wrap the data with domain specific functionality (#3).  When complete, you can use models from your controller like:</p>\n\n<p>Todo.findAll({after: new Date()}, myCallbackFunction);</p>\n\n<p>Finally, once your todos come back, you have to display them (#4).  This is where you use JMVC's view.  </p>\n\n<pre><code>'.show click' : function(el, ev){ \n   Todo.findAll({after: new Date()}, this.callback('list'));\n},\nlist : function(todos){\n   $('#todos').html( this.view(todos));\n}\n</code></pre>\n\n<p>In 'views/todos/list.ejs'</p>\n\n<pre><code>&lt;% for(var i =0; i &lt; this.length; i++){ %&gt;\n   &lt;label&gt;&lt;%= this[i].description %&gt;&lt;/label&gt;\n&lt;%}%&gt;\n</code></pre>\n\n<p>JMVC provides a lot more than architecture.  It helps you in ever part of the development cycle with:</p>\n\n<ul>\n<li>Code generators</li>\n<li>Integrated Browser, Selenium, and Rhino Testing</li>\n<li>Documentation</li>\n<li>Script compression</li>\n<li>Error reporting</li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/32540', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3436/']