First of all, I'm not really a Javascript guy, but more towards PHP.
However, I want to move to the next level, that is taking load off the server and let the client do the processing. Hopefully to be a more JS guy.

My first thought is to request of data off the server, and it just returns key-values which the client will generate the HTML and puck in the values.

There are 3 problems generally:

  1. I find it so much easier to generate the HTML in PHP cause data exchange between the server and HTML is fluid. Unlike returning data in JSON, which I then have to select the DOM that I want, and put in the values eg. $('.personRecord').html(data['name']).
    I also find looping through arrays and generating HTML easier than JS?

  2. I find myself using so much of jQuery selector to select the element I'm interested to manipulate and I'm not sure if that's the correct practice (going forward to be a JS ninja). But I'm doing it cause it sure saves lots of characters.

  3. In order to select the elements easily, I tend to give them unique class names as identifier. Again, I'm not sure if that's what a JS Ninja will do.

Going forward (web trend), I would like to know if I should try to put more work to JS?
Also, any ideas or thoughts on how I can overcome my 3 problems stated above?
Is it a sin to return formatted HTML as part of a JSON value? Or should HTML be generated in JS and only read values off the returned JSON when it needs?

Putting frameworks aside (I know there's Backbone, Angular, Can.JS and many more) does anyone really do raw JS scripting? I mean, it is possible but seems extremely wordy.

Lastly, please advice how one would convert a server centric site to a JS centric one?

If what I wrote doesn't make any sense, hopefully you can point some JS centric sites, and explain what approach they are using as an example. Sites that are full featured (say..maybe facebook, twitter, stackoverflow) and not just a TODO web app.

closed as not a real question by thorsten müller, Bart van Ingen Schenau, Jalayn, maple_shaft Apr 3 '13 at 22:45

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  1. jQuery is a fantastic DOM manipulation tool. What I have found though is that it is helpful to pair it with a good JavaScript templating tool (Knockout.js is one of my favorites because of the declarative data binding-like workflow it uses) precisely to do as little low-level manipulation as is possible. That said, jQuery has fantastic array looping with .each().

  2. The selector is the correct practice for retrieving DOM bits to manipulate. However, I would refer back to #1 for how to avoid doing repetitive manipulations.

  3. CSS classes should be applied to groups or things that may potentially become groups. If you really need to refer to an item directly, use an ID. It is typically preferred to group things with classes and seldom think in terms of manipulating some precise element.

Raw JavaScript (without even jQuery, Prototype JS or MooTools) can be written and can be used to manipulate the DOM. This is seldom done in practice because of the DOM implementation incompatibilities that a good framework will hide for you. If you write raw JavaScript you will quickly find that you are spending a good deal of valuable development time rediscoving oddball JavaScript/DOM incompatibilities.

As for how to perform such a conversion, there are two angles to consider: project management and technological. From the first perspective, you probably do not want to do some wholesale conversion. Rather, convert individual pieces as you fix bugs or augment their functionality.

The question lacks enough detail to provide a good technical answer. Moving towards an MVC-like architecture might be helpful. I have found web services, even very small ones, extremely helpful as they allow me to serve up fairly "dumb" pages (even if they are presented by something less-than-dumb like ASP.NET) and have the JS call off to the service to fill in data and perform operations.

Finally, you may want to step back and ask yourself why you are trying to make this transition. Better performance? Better user experience? What problem does it solve? There may be other solutions to your immediate problem.

Bear in mind that client-side scripting is no panacea. There are trade-offs. You need to be even more careful in your handling of data scrubbing and validation (which you should be doing anyway) and ensure that both your client and server side code can gracefully recover from errors presented by the other.

  • Thanks for the explanation. With regards to point 3 of your reply, I'm spamming the objectID in every element that requires interaction to my HTML. Because this page displays a list of objects and each list has a set of actions that should (with common sense) affect only the object it is displayed along with. I could group them under a ID, but then on every click action, I would have to check the parent, and to the ID. Not sure which is better though. Understand my question is too general and broad cause I was hoping to grapse an overview of how a JS centric site is built. – resting Apr 4 '13 at 3:13

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