Note that there are some explanatory texts on larger screens.

plurals
  1. POCode organization in JavaScript: MVC? Self-rendering components?
    primarykey
    data
    text
    <p>I'm building an extremely JS-heavy web application. I say it's JS-heavy, because the vast majority of the work going on is done on the client (although there is some syncing back and forth to the server using AJAX and XMPP).</p> <p>This is my first time building something of this scale in pure JS (using jQuery), so I began by organizing my code using MVC in a manner modeled after Rails. For example, if the user clicks a button, a method is called in the Controller object, which pulls some data from the models, and which then passed the data a view function. I do this for pretty much everything, even trivial actions like showing a small popup.</p> <p>After a few months of this, I feel like I'm not taking full advantage of the language. Should I really be rendering my views as if they are webpages?</p> <p>It seems like it'd make more sense to break down the views into components, which would be instances of Javascript objects/functions. For example, instead of...</p> <pre><code>var itemListHTML = '&lt;ul id="item-list"&gt;&lt;li&gt;item1&lt;/li&gt;&lt;li&gt;item2&lt;/li&gt;&lt;/ul&gt;'; jQuery('#container').html(itemListHTML); </code></pre> <p>...I could instead have...</p> <pre><code>components.itemList.render(); </code></pre> <p>So here, there's just a component called <code>itemList</code>. Since all the data is stored on the client, these components could instantly access all of the data necessary to create and manage themselves. I imagine I would still use MVC, but there'd be no need for controller actions that are responsible for the entire view. If I want to refresh one part of the UI, I just call <code>whateverComponentControlsThatArea.redraw()</code> and it re-renders itself.</p> <p>I'm sure someone had done this before. Is there a name for this style of code organization? Any guides or best-practices for how to implement it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload