jQuery Spaghetti! tips and tricks for cleaner codeJoin Stack Overflow to learn, share knowledge, and build your career. In a previous questionI asked how to remove useless SPANs from a string and the result was to use:. Here is a quick demo: Clean html jquery personally would just clean up the HTML, though. This is silly, especially considering how trivial it is for the browser to handle it as it is.
jQuery empty() Method
Last week I was at Confoo to present my talk, jQuery Spagetthi. This talk was a collection of best practices with jQuery, since it was somewhat popular why not put that in blog post form. When you work as a front-end developer in one of those agencies and you do website maintenance you always open the js folder in fear. Generally you find a couple of plugins and one file called, custom.
When you open that, sometimes you are lucky and it only has a couple of lines. Even if the code looks like crap, well, you could recode all this in a couple of hours. At the end of your day working in that mess, you want to become an alcoholic and change job, illustrated below. Basically, any event fired on any element in the dom can be cached on any parent of that element. More information on event delegation can be found here. This jQuery feature is generally underused in a general matte.
The first big use case of custom events is to decouple your code from your 3rd party plugins. The best example is the lightbox. The are several lightbox plugins, and it is not because you use one of those now that a better might popup later, or might break with ie Using custom events you could create events that you decide and use them in your code and only binding those events to your 3rd party in one place.
Since you bind it only once, changing the plugin is really easy and does not consist of 30 find and replace actions. Plus, you build a standard for your company and make it easier for employees to pick up the standard.
The second use case is for decoupling your js modules. Imagine what happens if you directly call functions from another module and that module is removed from the application?
See the example below: You want to modify the name of your user but also have another module where the name is printed. In this case you have 2 choices. If you do that, there is never other ways to change your name — you can just call that change: One of the big hurdle when you start with jQuery is selector traversing, most people stop at the first option,.
Unfortunately using it is not far from being an anti-pattern. What happens if another dev add a div between your button and your container? Your code is broken, and worse, no one even knows it. Fortunately there is an easy solution: Fortunately, deferred changed all that. Look at the example below:. Imagine you have 2 ajax requests at the same time, and you want to execute something after those 2 ajax requests have been executed.
I know talking about code structure is kind of a picky subject. Everyone does their own thing and well, I do not want to get into that. But for the love of god, do not put a endless series of event directly into the dom ready!
Please at least use objects to structure all that. I always bind a js module to a part of html document, in init I check if my html container is in the dom. If it is not there, full stop. The rest of the module will not be loaded, and this reduces the overhead and leakage I could get if the module is loaded in another part of the application where it should not be loaded or if all the js is concentrated in one big js file.
All our events in here are loaded with delegate using the html container that I saved earlier in init. We also have a custom event modal. The pattern above is one of the easiest and simplest one you can use. Of course there is other solutions out there where you can get private and pseudo protected space. One of my favorite is the revealing pattern, another pattern much underused is the jQuery plugin pattern.
Email Marketing jQuery Spaghetti! Probably not what you want to do in your code.. So what we are going to look at is how you can improve on the mess.. Events jQuery offers you 3 types of event handler, you get bind , binded directly on the html element and need to be there when bind is loaded live , that use event delegation and is binded directly on the document delegate , like live use event delegation but with a context, can be binded on any element in the DOM Event Delegation, bubbling — what is this?
Abuse Delegate You should probably always use delegate as it has a lot of advantages over bind and live. Bind is slow when used on a collection of html objects, example, you bind something to 20 inputs, it will have to actually parse all your html documents and bind 20 times your event for each element. Delegate does not suffer from this, you bind it only one time on the form, and let the event delegation magic do the rest. Imagine you use live on a button having a class.
Custom Events This jQuery feature is generally underused in a general matte. Trending Now The iframe cross-domain policy problem January 24, Chrystian Guy January 12, Kevin Huxham November 17, Chrystian Guy October 27,