12.07.2015 Views

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9 WRITING BETTER JAVASCRIPTUnderstanding Directly Resolved DependenciesThe tiny web app works because jQuery has been loaded long be<strong>for</strong>e I call the map<strong>Pro</strong>ducts function. Thesituation would be different if I rewrote the web app to use a different toolkit. Most programmers do thesame thing when they first understand that assumed dependencies are a problem: they assume controlof the situation and take direct action to fix it. Listing 9-9 shows a typical solution.Listing 9-9. Taking Direct Action to Resolve Assumed Dependencies(function() {function createNamespace(namespace) {...code removed <strong>for</strong> brevity...};var utilsNS = createNamespace("cheeselux.utils");Modernizr.load({load: 'jquery-1.7.1.js',complete: function() {utilsNS.map<strong>Pro</strong>ducts = function(func, data, indexer) {$.each(data, function(outerIndex, outerItem) {$.each(outerItem[indexer], function(itemIndex, innerItem) {func(innerItem, outerItem);});});}}})...code removed <strong>for</strong> brevity...})();In this listing, I have taken responsibility <strong>for</strong> resolving my dependency on jQuery by usingModernizr to load it be<strong>for</strong>e creating my map<strong>Pro</strong>ducts function. (The load property in a Modernizr.loadobject specifies that the <strong>JavaScript</strong> file should always be loaded.)In doing this, I have trans<strong>for</strong>med an assumed dependency into a directly resolved dependency. Adirectly resolved dependency is when I rely on another <strong>JavaScript</strong> library and I take direct action to makemy code work, usually by loading the library myself.Understanding the <strong>Pro</strong>blems Caused by Resolving a DependencyDirectly resolving a dependency causes a lot of problems. First, I created an assumed dependency onModernizr to ensure that jQuery is loaded, which isn’t a huge step <strong>for</strong>ward. But the real damage is that Ihave made sure that the map<strong>Pro</strong>ducts function works; however, in doing so, I have undermined thestability of the web app itself.To see the problem, load the web app, and reload the page a few times. There are two issues. If theweb app works, you have encountered just the least serious one, which is that the jQuery library hasbeen loaded twice. You can see this in the browser developer tools or in the console output from theNode.js server that prints out each URL that is requested. Here is the list of files loaded by the web app asreported by the server, with annotations to highlight the two loads <strong>for</strong> jQuery:240www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!