How to abstract the data away from the design in web pages? - javascript

What are ways of keeping the data separate from the design of a webpage, so that if you redesign the site, or you want to provide the ability for users to customize the layout, it would be really easy to do so.

I think your question is confusing for most of others here. I see a lot of irrelevant answers coming up with "MVC" while you actually mean "separate content from style" instead of "separate data from design" which could be incorrectly interpreted as "separate model from view". The first part of your question indeed gives the impression that you're looking for MVC, but the second part of your question and the tags used made me realize that you actually didn't mean that.
The answer is simple: just don't use inline CSS (such as style="color:red") but have it in an entirely separate stylesheet which you include in the HTML head. Give the HTML elements sensible ID's and/or classnames. You can let the CSS hook on that. Also use HTML wisely and semantically. Separate the content in positionable block elements. Don't use tables for layout.
Certainly checkout the CSS Zen Garden as someone already mentioned before me. It uses exactly the same content (HTML code) throughout many different styles which you can select from a menu.
For more interesting blogs/links you may find those Google searches useful:
html semantics
css naming conventions
The same story also applies on JavaScript by the way. Do not use inline JS code, but just have it in an entirely separate file and make use of unobtrusive Javascript as many as possible. I.e. your website should still be useable without Javascript. The jQuery library is a perfect choice for that. Its selectors are also based on having sensible ID's and/or classnames.

The most common approach these days is to use the Model-View-Controller pattern.

Take a look at CSS Zen Garden and use the View Source feature.
I take this question as "provide the ability for users to customize the layout".
Such is the job of CSS, to provide "style" separate from HTML "markup".
With careful designs (with the stated goal in mind).
You can craft your markup so it'd can be really easy to style and re-style over and over.
It's not hard, I've done it a few times on a few redesigns.
You just need to stick to the semantics as much as possible.
On the backend, though, separating the model from the code is pretty much a nailed down science by now depending on what your backend infrastructure looks like and how far you want to go.

As David said, use the MVC pattern. But the biggest help is design the system early to avoid issues with bind your data (sources) to your interface. Make your interfaces so that you can change them out quickly etc.

The answers suggesting the use an MVC pattern are 100% right, and I encourage you to embrace it too.
More specifically, the kind of technology usually employed to obtain what you are seeking is the use of a templating system (such as Smarty if you use php, for example).
A web application framework (such as Cakephp, Rails or Django) can help you get started and achieve proper separation, usally with little effort.
The only drawback is that a change in approach and/or mentality may be required :)

Related

What are the benefits of keeping all stylings in CSS files when doing Javascript intensive work?

There seems to be the belief out there that ALL stylings need to be kept in the CSS files. If I am doing purely JavaScript work, this does not make sense to me. If I create a JavaScript object called : "StatisticSummaryPanel", surely it is better to keep the CSS stylings inside the JavaScript object. That way I only have to maintain one file instead of two files.
It's generally considered better practice to have your CSS, JavaScript and HTML in separate files as it makes it easier to locate, understand and debug for yourself and for potential future developers who come along and work on your files.
In your JavaScript file it's just a cleaner approach to simply refer to a class or an ID that has it's properties stored in the CSS file.
If you reuse a class for example in multiple JavScript objects and you need to change the properties of that class then you only have to update the CSS file.
It helps if you need to reuse the same style in a different page/javascript file. This way if your style changes, you need not change in all the files you use it.
Also, it can be cached and make your page little faster.
And coming to maintain more files, its a call based on size of your files. If it's too huge, a single file will be costly to maintain.
There are several problems:
1. Code legibility
Writing CSS or HTML in Javascript is verbose and difficult to read and edit compared to when written directly in its own syntax.
2. Confusing behaviour and logic
A bare minimum of styling is necessary to achieve basic functionality, but appearance and logic shouldn't be confused. Figuring out how and where to integrate code about appearancee into code about behaviour is a non-sensical task. Your code will be much clearer without esoteric style requirements baked into your behavioural scripting.
3. Retrieval
People don't expect to find CSS in Javascript. When someone wants to try and change the appearance of the StatisticSummaryPanel, they're going to look for it in the CSS. The last place they'll expect to find its stylistic definition is in the JS.
Separation of concerns: in this case, the separation of presentation (how various effects appear and render) and behavior (the causes of said effects). In our experience, separating the two allows us to hire great graphic designers for the former, and great programmers for the latter.
For a small project, it probably doesn't matter, as you're the only one responsible for the code. But if you plan to expand to a team, it's worth thinking about how different members may specialize, and how best to coordinate changes amongst your teammates.

Javascript: How to make sense of all the frameworks and design philosophies?

I have been a user of jQuery (and some of its minor plugins) for a while. The Javascript code I've developed over the years could be described best as... messy. It used a ton of global variables and functions here and there, didn't use standard ways of organizing the code, nor any design patterns whatsoever.
I am currently building the new version of a website, and I have completed doing the backend with PEAR::MDB2 and Smarty templates. The rest is just homebrew PHP with some classes.
Now I am at the point where I'll add the Javascript layer on top of the website to improve the user-friendliness of some features. (while making sure everything degrades gracefully) I want to write better, cleaner, more organized Javascript than I used to, so I did a little research. I read Stefanov's Object-Oriented Javascript to have a better grasp on some concepts I knew only loosely about (prototypes, constructors, etc.) as well. Now I'm stuck at a point where I wonder which Javascript frameworks I should use, and how to organize it all.
After conducting my research, I understood Cappuccino & Objective-J, and Sproutcore were not what I was looking for. To quote Cappucino's about page:
Cappuccino is not designed for building web sites, or making existing sites more "dynamic". We think these goals are too far removed from those of application development to be served well by a single framework. Projects like Prototype and jQuery are excellent at those tasks
So there's that. Then I found out about Coffee Script, which is more of a one-to-one "compiler" and wouldn't help me with the actual organization of my code.
I also stumbled on some articles that give guidelines:
Using Inheritance Patterns to Organize Large jQuery Applications
A JavaScript Module Pattern
I also found out about Backbone.js, Shoestring, JavaScriptMVC, Google Loader, jQuery Tools, jQuery UI. I don't really know what to do of all this... The things I know:
I don't want to invest too much time in learning something too complex, I want to keep things simple and flexible as much as possible (that is why I don't use Symfony on the backend, for example), yet clean and organized.
I want to use jQuery, the question is, what should I use with it? (that is compatible too)
Right now, I'd use jQuery and jQuery Tools and "organize" all that in a simple namespace/object literal with simple properties and methods and also, since the site is localized, I just plan on using the simple vsprintf (as I do on the backend) with key:value pairs loaded from an object literal provided by the backend. JavaScriptMVC seems interesting, but I fear it would bring way too much complexity for a project that is fairly small sized. That is where I need your advice! Thank you very much in advance.
Ok, my attempt at an answer:
There is no 'best' to way to do it. You now know what's there and I think you might have a preference for yourself for what you want. In that case, pick a framework and learn it inside-out. (sorry to burst your bubble, but each framework has a learning curve, some steep, some very easy, but in the end to use it well you have to invest in it. Just do it, you won't be sorry).
You of course have an preference for clean code, so you might take some considerations into account. You also say you have a preference for jQuery, which is fine, but there are some limitations (as also pointed out in the link provided by eskimoblood).
There are some nice lectures / and tutorials with advice on how to structure your code in jQuery:
How to manage large jquery apps
On Large jQuery apps
Essential Javascript and jQuery patterns (free ebook)
Some style guides:
Jquery core UI Styleguide
Google Closure Javascript Style Guide
Tools for checking your code
JSLint
JSHint (a more forgiving/practical fork)
Closure Linter (haven't tried it yet, but intend to)
Standard works (javascript)
Everything by Douglas Crockford
Quirksmode
There might be more.. perhaps more people can contribute, but I also think that you've almost reached the end of what you can learn before getting your hands dirty. Many of these guides are written in a very generic way, but the interesting thing is that javascript is called upon in many specific situations. It might be useful to just post some of the code that you regard as "messy" and we can help you figure out how to do it better. Good luck!
You should watch the video and read the links in this article and then you should ask yourself again if jquery is the right tool. Maybe you will use dojo, that is much better for larger projects or you take a look at backbone and where you can stay with jquery. After all both of them are more "javascriptish" then something like sproutcore, cappuciono or even GWT. And also much easier to understand when you come from jquery.
One framework that is to consider is definitely ReactJS from Facebook. This framework is pretty slick in many ways.
First thing you have to know is that it is a view framework. It can be used server-side to do the pre-rendering of pages, but it really shines on client side. Since it's a view framework, it can be used with backbone or any other "back-front"-end framework.
One of the main point of React is its rapidity. It keeps a virtual DOM in memory and virtualize all the webpages events. So the virtuals event are used to keep events browser agnostics.
The virtual DOM kind of make programming a dynamic site as if you were programming an old static website. You can just shoot the whole HTML to render to the view engine (as if you were "re-rendering" the whole page) and it will manage the DOM operations. It does a diff between the new virtual DOM and the current virtual DOM and only inserts nodes that needs to be inserted. This way you reduce the number of DOM ops and thus increase your render speed by a lot.
A good place to start is this tutorial which shows how to use "Flux" (the web flow designed by Facebook for its site) in order to realize a Todo application!

Wrap jQuery, dojo with custom library?

Coming from Java, I'm wondering if a Java best practice applies to JavaScript.
In Java, there's a separation of interface and implementation, and mixing them up is considered a bad practice. By the same token, it is recommended to hide implementation details of your library from end developers.
For example, log4J is one of the most popular logging libraries out there but it is recommended to write code to the slf4j library or the Commons Logging library that "wraps" log4j. This way, if you choose to switch to another logging framework such as logback, you can do so without changing your code. Another reason is that you, as a user of a logging library, how logging is done is none of your concern, as long as you know what logging does.
So back to JavaScript, most non-trivial web applications have their own custom JavaScript libraries, many of which use open source libraries such as jQuery and dojo. If a custom library depends on, say jQuery, not as an extension, but as implementation, do you see the need to add another layer that wraps jQuery and makes it transparent to the rest of JavaScript code?
For example, if you have the foo library that contains all your custom, front-end logic, you'd introduce the bar library that just wraps jQuery. This way, your foo library would use the bar library for jQuery functions, but it is totally oblivious to jQuery. In theory, you could switch to other libraries such as dojo and google web toolkit without having a big impact on the foo library.
Do you see any practical value in this? Overkill?
Although it makes sense from a theoretical standpoint, in practice I'd say it's overkill. If nothing else for these two reasons:
Anything that adds to the size of
the request (or adds more requests)
is bad - in web world, less is more.
If you're using say jQuery, the
chances of you switching to
something like Mootools is (imho) slim to none. From what I've seen, the top libraries each aim to solve different problems (at least in the case of Mootools and jQuery - see this great doc for more info on that). I'd assume that you'd incur a tremendous amount of headache if you were to try to implement a middleware library that could easily switch between the two.
In my experience and being a Java developer myself, sometimes we tend to take the whole "abstraction" layer pattern too far, I've seen implementations where someone decided to completely abstract a certain framework just for the sake of "flexibility" but it ends up making things more complicated and creating more code to maintain.
Bottom line is you should look at it on a case by case basis, for example you wouldn't try to create an abstraction layer on top of struts, or on top of JPA, just in case you then go to a different framework (which I've rarely seen done).
My suggestion is, regardless of the framework you are using, create objects and components that use the framework internally, they should model your problem and be able to interact between them without the need of any specific framework.
Hope this helps.
There are a lot of good answers here, but one thing I don't see mentioned is feature sets. If you try to write a library to wrap the functionality provided by, say, jQuery, but you want to be able to easily swap out for something like prototype, you have a problem. The jQuery library doesn't provide all the features prototype provides, and prototype doesn't provide all the features jQuery provides. On top of that, they both provide their features in radically different ways (prototype extends base objects -- that's damn near impossible to wrap).
In the end, if you tried to wrap these libraries in some code that adds 'abstraction' to try to make them more flexible, you're going to lose 80% of what the frameworks provided. You'll lose the fancy interfaces they provide (jQuery provides an awesome $('selector') function, prototype extends base objects), and you'll also have to decide if you want to leave out features. If a given feature is not provided by both frameworks, you have to either ditch it or reimplement it for the other framework. This is a big can of worms.
The whole problem stems from the fact that Java is a very inflexible language. A library provides functionality, and that's it. In JavaScript, the language itself is insanely flexible, and lets you do lots of crazy things (like writing a library, and assigning it to the $ variable). The ability to do crazy things lets developers of javascript libraries provide some really creative functionality, but it means you can't just find commonalities in libraries and write an abstraction. I think writing javascript well requires a significant change in perspective for a Java developer.
Someone wise once said "premature optimization is the root of all evil." I believe that applies in this case.
As others have expressed, you don't want to abstract for the sake of flexibility until you have an actual need for the abstraction. Otherwise you end up doing more work than necessary, and introducing unnecessary complexity before it is required. This costs money and actually makes your code more brittle.
Also, if your code is well organized and well tested, you should not be afraid of major changes. Code is always changing, and trying to anticipate and optimize for a change that may or may not come will almost always get you in more trouble than it saves you.
Acknowledgement: I should give credit to Agile programming and my practice and readings on the topic. What I've said comes directly from my understanding of Agile, and I've found it to be an extremely good razor to cut out the extra fat of my work and get lots done. Also none of what I've said is actually JavaScript specific... I'd apply those principles in any language.
There are good arguments calling this development practice - wrapping in order to switch later - into question in any language.
A good quote by Oren Eini, from his writeup on wrapping ORMs:
Trying to encapsulate to make things
easier to work with, great. Trying to
encapsulate so that you can switch
OR/Ms? Won’t work, will be costly and
painful.
This is definitely something that is done in enterprise environments.
Take for example a company that has their own custom javascript framework that is used on all of their projects. Each of the projects decide to use their own framework (jQuery, Dojo, Prototype) to add functionality to the underlying modules of the company framework. Employees that move between projects can now easily do so because their API with working the project's codebase is still the same, even though the underlying implementation could be different for each project. Abstraction is helpful in these situations.
It is overkill. Javascript is not Java and is not in any way related to Java. It is a completely different language that got J-a-v-a in the name for marketing reasons.
If you are concerned with availability of add-on libraries, then choose a framework with a large ecosystem. In an enterprise environment you will be further ahead by standardising on a vanilla off-the-shelf uncustomised web framework that you can upgrade every year or so tracking the rest of the world. And then supplement that with a SMALL in-house add-on library which you will, of course, have to maintain yourself, not to mention training any new programmers that you hire.
Since you are talking about Javascript in the client (web browser) it is more important that you limit the complexity of the things that people do with it. Don't build huge amounts of client side code, and don't make stuff that is so brittle that another programmer can't maintain it. A web framework helps you both keep the linecount down, and keep your own code reasonably simple.
It is not a question of Javascript best practice, because that would be different for server-side JS such as Rhino or node.js.
Adapter pattern is not a common solution in this case. The only example I know to use this pattern is extjs. Javascript projects are usually too small and they aren't worth the effort you would make by creating such an abstraction layer.
The common solution for this problem is that you try to use multiple frameworks together for example with jquery.noConflict.
I've done this before, and can talk a bit about the experience of writing a library/toolkit wrapper.
The plan was to move from Prototype to some other library. Dojo was the first choice, but at the time I wasn't sure whether that's the library to move everything to (and by everything I mean ~5MB of Prototype-happy JS). So coming from a world of clean interfaces, I was set to write one around Prototype and Dojo; an awesome interface that would make switching out from dojo a breeze, if that was in fact necessary.
That was a mistake that cost a lot of time and effort for a few reasons. The first one is that although two libraries can provide the same functionality, (a) their API will almost always be different, and most importantly (b) the way you program with one library will be different.
To demonstrate, let's take something as common as adding a class-name:
// Prototype
$("target").addClassName('highlighted');
// Dojo
dojo.addClass("target", "highlighted");
// jQuery
$("target").addClass("highlighted");
// MooTools
$('target').set('class', 'highlighted');
Pretty straight-forward so far. Let's complicate it a bit:
// Prototype
Element.addClassName('target', 'highlighted selected');
// Dojo
dojo.addClass("target", ["highlighted", "selected"]);
// jQuery
$("target").addClass(function() {
return 'highlighted selected';
});
// MooTools
$("target").set({
"class": "highlighted selected"
});
Now after choosing an interface for your version of the addClass you have two options: (1) code to the lowest common denominator, or (2) implement all of the non-intersecting features of the libraries.
If you go with the 1st -- you'll loose the "personality" / best qualities of each of the library. If you go with #2 -- your addClass' code will be at 4 times larger than the ones provided by any of the libraries, since for example when Dojo is included, you'll have to write the code for the function as the first param (jQuery) and the Object as the first param (MooTools).
Therefore, although it is theoretically possible, it isn't practical, but is a very nice way to understand the intricacies of the libraries out there.

Should a web designer know JQuery?

I have been doing web designing for quite some time. My question is:
Should a web designer know JQuery at all?
Thanks
Not necessary but it is good to have that weapon in your web design arsenal if you want to make your web pages full of life.
In fact, Jquery has been made such that it becomes very easy for the designers to get started with it. For example, see its selector machensim, it is pretty similar to CSS selectors, something designers are always used to.
For example, Chris Coyier is famous web designer and guess what he knows JQuery and he creates amazing lively pages.
Finally, have a look at this great resource:
jQuery for Designers
Most of the people I know with the job title "Web Designer" (as opposed to "Web Developer") are concerned only with design and not with the technical details of implementation.
So, for their jobs, it is important to know what jQuery can make possible (i.e. nothing that they shouldn't already know is possible from a broader understanding of what JavaScript can achieve), but not how to use it.
More knowledge is always good, but a designer would probably be better off learning more about subjects such as usability, accessibility, information architecture, colour theory and so on first.
My personal view is that a web-designer shouldn't be expected to know how to program in jQuery. However, I do think they should:
Have a good awareness of what jQuery is, what it is useful for and how it can be used appropriately to enhance a site. This also means understanding what cannot be easily done with jQuery. Be aware of the possibilities and the limitations.
They should be able to integrate and style off-the-shelf plugins that don't require much more than following basic instructions.
A good designer must keep up with how jQuery is being used "out there" in the web, enabling them to see how it can enhance interfaces and solve common problems. Like everything in the fast-moving world of the web you need to keep on top of the trends and also be able to distinguish between fads and genuine innovation.
HTML is about structuring content
CSS is about presenting your content
JQuery is about behavior and interaction with content
A good designer should be able to deal with challenges in all 3 areas.
First, some traditional analogies:
Should a race car driver know how the race car engine works?
Should she know how to fix it and tune it?
Should she know how to improve the engine and design a new part for it?
The answers:
No. Not necessarily.
Could it help, in certain situations, to:
increase productivity,
make html and css cleaner,
make better visual design decisions,
make better interaction design decisions,
better understand what is possible to design (improve creativity)
Yes. Yes definitively.
Web Designer should (must?) know jQuery exist. Should also know its basic capabilities so that his designs could include some cool stuff that is really easy to do with jQuery.
I wouldn't expect much more from a designer.
No. I don't think it's a part of designing if you know its additional knowledge you have & it also helps you some other way. But i think javascript & javascript libraries are not a cup of tea of Web Designer. I think you should concentrate on CSS, HTML & if possible FLASH.
If you're trying to get by in the computer industry by learning as little as possible, I think you're setting yourself up for career suicide.
Given two web designers of approximately equal design ability, whom would you pick for your project? The one with Javascript knowledge or the one who doesn't?
If a web designer is a programmer or want to be one, yes. But if not, he/she should not learn it, separation of concerns. I wanted to close this question, seems not a programming one, but I'm interested to read others' ideas :-)
But based on Sarfaz provided link, i think you should learn it. jQuery seems, is like the Excel Macro of web, you need to learn some Macro programming in order to spiffy up your spreadsheet(aesthetic-wise and functionality-wise), in the same vein that the best way to spiffy up your website appeal is by using jQuery

patterns/patterns for designer-webdeveloper communication

as the title implies i am searching for a good pattern / schema collection that makes designer <-> webdeveloper communication less ambiguous when it comes to javascript effects and dynamic content loading.
google just provided me with the shorthand markup by ryan singer, but this seems to be restricted to page/ui flows.
can you recommend any commonly used / efficient patterns?
Have you tried the yahoo patterns repository?
A goal for site implementation should be to minimize your on-page scripting. People call that "unobtrusive Javascript" or something like that. In order to do that, page elements will need some markup for your script code to look for anyway. In other words, if you want to have a "div" that fades in after the page loads, well then you might add to its "class" value something like "fadeIn fadeSpeed:slow". Your script code will look for such things and perform animations (or whatever).
Well the point is that as you set up such a framework, you'll naturally end up with a way for the designer to communicate intention. You'll have to come up with a "vocabulary" of page dynamic elements, but you'd hopefully do that anyway. Once that's done, then the very thing that the markup will need to actually make the dynamic effects happen will serve as the designer's vehicle for indicating design intentions.
Another useful tool for such things is the development of your own custom tags, something that varies between difference server platforms. Such tags can "wrap" certain messy markup tasks and, again, capture the agreed-upon site elements that implement an overall design theme.

Categories