SlideShare a Scribd company logo
1 of 109
Download to read offline
Friday, 3 June 2011
“Always walk through life as if you have
                       something new to learn and you will”


                                       It wasn’t me




Friday, 3 June 2011
Who Am I?

                      •   Member of the jQuery /(Bugs|Docs|Learning|
                          Front-end)/ Subteams
                      •   Software Engineer at AOL

                      •   Writer (Essential JS Design Patterns, AddyOsmani.com, Script Junkie etc.)




Friday, 3 June 2011
AOL? You guys still exist?!

                        Yes, we do!
                                         We actually actively use many of the tools
                                                   I’ll be covering today.




Friday, 3 June 2011
Let’s rock!
Friday, 3 June 2011
jQuery is great..but!

                      • It doesn’t provide all the tools needed to
                        build serious JavaScript web applications
                      • It does give us easy access to DOM
                        manipulation, ajax , animation, events etc.
                      • But how do we fill in the gaps for
                        everything else?



Friday, 3 June 2011
Let’s evaluate what we’re missing by
                       building a hypothetical application




Friday, 3 June 2011
StalkBook!




Friday, 3 June 2011
What does it do?

                      • Enter a friend’s name to find accounts that
                        match it on other social networks
                      • Allows sharing different views of results easily
                      • Remembers user’s last known configuration

                                    Jon Stewart



Friday, 3 June 2011
btw fool..it also has to..

                      • Be quick and support easily changing
                        behaviour later
                      • Offer multiple views of the data, but
                        require no hard page refresh
                      • Run without any database on our end
                      • Be well tested & perform optimally
                                            Mr. T, Project manager
Friday, 3 June 2011
Some implementation notes

                      • Could be a single-page application
                      • Separate application concerns & keep the
                        code decoupled
                      • Store persistent configuration information
                      • Make best use of modern technologies to
                        improve the user experience
                      • Work cross-browser with fallbacks for IE
Friday, 3 June 2011
What do we have to work with?




Friday, 3 June 2011
jQuery can give us this
                                                                 Third-party API


                                                        server



                                            Ajax


                      DOM Manipulation
                                                                  UI


                                          Cookies
                                         (via plugin)




Friday, 3 June 2011
But we’re missing this
                        Browser State        Unit Testing            Architecture
                         Management                                 patterns (MVC)
                                                                                      Cross-browser
                                                                                     persistent storage

          HTML5/
            CSS3
                                                Application logic
           Feature
          Detection
                                                                                         Build Process
                                                                                        & Optimization

                       Client-side      Communication
                      Templating *                           Dependancy
                                           patterns
                                                             management          Widgets
                                          (pub/sub)



Friday, 3 June 2011
Let’s fill in the gaps, bro!
Friday, 3 June 2011
Code Organization:
                                Design Patterns

                      • Reusable solutions to commonly occurring
                        problems in software design
                      • Proven approaches that reflect experience
                        and insights of developers over time
                      • Highly flexible & can be adapted to our
                        own needs easily



Friday, 3 June 2011
I can haz architecture?
                      • Architecture patterns describe patterns
                        for broader systems with a larger
                        scope.
                      • Certain patterns can be applied to both
                        the server-side and client-side
                      • Let’s look at one you know

Friday, 3 June 2011
The MVC Pattern
                      • Separates objects into three main
                        concerns: models, views and controllers
                      • Traditionally heavily used on the server-side
                      • Excellent for code-organisation in general
                      • Implementations can vary quite significantly

Friday, 3 June 2011
Models,Views & Controllers

                      • Models represent knowledge and data (eg.
                        get and set methods). Isolated from Views
                        and Controllers
                      • Views can be considered the UI. Generally
                        ‘dumb’. Don’t need to perform validation
                        logic
                      • Controller sits between Models and Views.
                        Performs business logic, data manipulation


Friday, 3 June 2011
MVC Options

                      •   JavaScriptMVC

                      •   Backbone.js + Underscore

                      •   Spine.js

                      •   SproutCore

                      •   Sammy.js




Friday, 3 June 2011
JavaScriptMVC
                                                            Lead: Justin Meyer




                      • JMVC can really be considered two things:
                       • Repeatable MVC architecture
                       • All-in-one integrated development tools
                      • Benefit is that it provides a clear path to
                        adding functionality
                      • Does a lot of the things you should be
                        doing in your project anyway


Friday, 3 June 2011
JMVC Components
                      • JMVC is broken down into 4 projects:
                        • jQueryMVC - MVC extensions for jQuery
                        • StealJS - Dependency management, build
                          process, code generators
                       • FuncUnit - A web testing framework
                       • DocumentJS - A JavaScript docs
                          framework


Friday, 3 June 2011
JMVC’s MVC

                      • Model: classical model layer.
                        organize Ajax requests and service data
                                                                  Effectively, a way to package and




                      • Controller: jQuery widget factory
                      • View: client side templating (eg.
                        jQuery.tmpl, Micro)
                      • Class: a JavaScript-compatible class system

Friday, 3 June 2011
JMVC Class
      $.Class("Speaker",{
          talk: function(){]
              console.log("I just said something");
          }
      });

      //usage
      var douglasCrockford = new Speaker();
      douglasCrockford.talk();

      //we can take this further as follows:
      Speaker("Organizer",{
          announce: function(){
              console.log("please take your seats");
          }
      });

      //usage
      var johnAlsopp = new Organizer;
      johnAlsopp.announce();
      johnAlsopp.talk();
Friday, 3 June 2011
JMVC Model
      $.Class("Model",{
          //get a speaker's slides
          getSlides:function(eventName, success){
           $.get('/slidesApi/'+ eventName, this.callback(function(q){
              success(new this(q));
             })
           },'json')
        }
      },{
          init:function(q){
           $.extend(this, q)
        }
      });

      Model("Crockford", {
          getRecentSlidesUrl: function(){
             return "My most recent slides are at:" + this.slideUrl;
          }
      });

      Crockford.getSlides('webDirections', function(crock){
          console.log(crock.getRecentSlidesUrl());
      });
Friday, 3 June 2011
JMVC Controller
      $.Controller('ModalWindow',{
        //sets up the widget
        init: function(){
           this.find('.modal').hide();
        },
        ".newModal keyup": function(el, ev){
            if(ev.keyCode == 13){
              new Modal({
                 title : el.attr('title'),
                 href: el.attr('href'),
                 complete : false
              }).save(this.callback('created'));
          }
        },
        ".modal .close click" : function(el){
            el.closest('.modal').model().destroy();
        }
      });




Friday, 3 June 2011
JMVC View
      //Views essentially use client-side templating
      //Here's a simple example

      <script type='text/ejs' id='slidesEJS'>
      <% for(var i=0; i < slides.length; i++){ %>
          <li><%=slides[i].name %> were talked about at <%=slides
      [i].eventName %>.

      A preview of the talk can be found below:<img src="<%=slides
      [i].thumbnailUrl %>"/></li>
      <%} %>
      </script>

      //Which can be easily rendered using..
      $("#slides").html('slidesEJS', slideData);


Friday, 3 June 2011
Why JMVC?
                      • Maturity & includes testing, dependency
                        management, build tools, client side
                        templates.
                      • Perfect for large applications where an all-
                        in-one solution is required.
                      • Scales well. It was most recently used by
                        Grooveshark in their application re-write.


Friday, 3 June 2011
GrooveShark




Friday, 3 June 2011
Backbone.js
                                                            Lead: Jeremy Ashkenas




                      • Backbone provides the ‘backbone’ you need
                        to organize your code using the MVC
                        pattern.
                      • Excellent for a lightweight solution where
                        you select the additional components you
                        feel work best for your project.
                      • Works best for SPAs (single-page apps)
Friday, 3 June 2011
Who uses it?
                         MetaLab



                                   SoundCloud




                      BitTorrent


Friday, 3 June 2011
Backbone’s MVC
                      • Models: represent data that can be created,
                        validated, destroyed & listened to for
                        changes. Collections are sets of models.
                      • Views: display the model’s data / provide
                        the user interface layer
                      • Controller: methods for routing client-side
                        URL fragments


Friday, 3 June 2011
Backbone Model
     window.Note = Backbone.Model.extend({
       // Default attributes for the note
       defaults: {
          content: "empty note",
          done: false
       },

        // Make sure each note has default 'content'
        initialize: function() {
           if (!this.get("content")) {
             this.set({"content": this.defaults.content});
           }
        },

        // Toggle the `done` state of this note
        toggle: function() {
           this.save({done: !this.get("done")});
        },

       // Remove this note from local-storage.
       clear: function() {
         this.destroy();
         this.view.remove();
       }
     });

Friday, 3 June 2011
Backbone Collection
     window.StickyNotes = Backbone.Collection.extend({

        // Reference to this collection's model.
        model: Note,

        // Save all of the note items under the `"notes"` namespace.
        localStorage: new Store("notes"),

        // Filter down the list of all notes that are finished.
        done: function() {
           return this.filter(function(note){ return note.get('done'); });
        },

       // Filter down the list to only note items that are still not finished.
       remaining: function() {
         return this.without.apply(this, this.done());
       }
     });




Friday, 3 June 2011
Backbone View
     window.AppView = Backbone.View.extend({
          el: $("#notesapp"),
          events: {
             "keypress #new-note": "createOnEnter",
             "click .note-clear a": "clearCompleted"
          },
         initialize: function() {
             _.bindAll(this, 'addOne', 'addAll', 'render');
             this.input    = this.$("#new-note");
             StickyNotes.bind('add',     this.addOne);
             StickyNotes.bind('reset',   this.addAll);
             StickyNotes.bind('all',     this.render);
             StickyNotes.fetch();
          },
          render: function() {
             this.$('#notes-info').html({ /* template logic could go here*/ });
          },
          addOne:function(note){
             //add a single note
             var view = new NoteView({model: todo});
             this.$("#note-list").append(view.render().el);
          }
          addAll: function() {
             //add everything to the stickynotes collection at once
             StickyNotes.each(this.addOne);
          },
     });

Friday, 3 June 2011
Spine.js
                                                                 Lead: Alex MacCaw




                      • A lightweight MVC framework with a focus
                        on providing an inheritance model through
                        classes and extension.
                      • Based on Backbone's API so developers
                        may find getting started a little easier than
                        expected
                      • Personally found it a nice alternative
Friday, 3 June 2011
Spine’s MVC
                      • Classes: traditional ‘classes’ powered by an
                        emulated version of Object.create
                      • Models: your application's data storage
                        models as well as any logic associated with
                        this data. Based on classes.
                      • Controllers: Similar to ‘Views’ - each
                        controller has an element associated with
                        it. Also based on classes.


Friday, 3 June 2011
Spine vs. Backbone
                      • Spine’s Controllers are effectively the same
                        concept as Backbone's Views.
                      • In terms of inheritance, Backbone uses
                        constructor functions and prototypes
                        whilst Spine uses an emulated version of
                        Object.create for a simulated class system
                      • Spine doesn’t require underscore as a
                        dependancy


Friday, 3 June 2011
Sammy.js
                                                               Lead: Aaron Quint




                      • A popular lightweight framework that allows
                        you to easily define route based applications.
                      • Some consider it the best controller
                        framework but it doesn't provide the Model
                        and View aspects itself.
                      • Good for SPAs requiring code organization.
                      • Also used by apps such as PaperlessPost
Friday, 3 June 2011
Sammy.js Example
     //initialize the application
     var app = $.sammy(function() {

           //set the plugin for providing create/render client side templates
           this.use(Sammy.Template);

           //handler for a page that doesn’t exist or can’t be located
           this.notFound = function(verb, path) {
               this.runRoute('get', '#/404');
           };

           //define a route for 'about'
           this.get('#/about', function() {
               //partial calls template() because of the file extension
               this.partial('templates/about.template', {}, function(html) {
                   $('#page').html(html);
               });
           });

           //define a route for '404' pages
           this.get('#/404', function() {
               this.partial('templates/404.template', {}, function(html) {
                   $('#page').html(html);
               });
           });

     });
Friday, 3 June 2011
PaperlessPost




Friday, 3 June 2011
SproutCore
                                                                     Lead: Charles Jolley


                      •   Recommended for applications wishing to have a
                          desktop-like ‘richness’.
                      •   Data binding framework makes updating views easy
                      •   Extends MVC to include a server interface, a display
                          that 'paints' your interface and responders for
                          controlling application state.
                      •   Often used for larger enterprise apps, but may be a
                          little overkill for smaller ones.
                      •   Apple used SproutCore for MobileMe


Friday, 3 June 2011
SproutCore Class + View
     //Root object for the application. SC.Application is usually needed if you
     //wish to use SC.Responder later to route events.
     Todos = SC.Application.create();

     //SC.Object is the root class for most classes defined by SproutCore.
     Todos.Todo = SC.Object.extend({
         title: null, isDone: false
     });

     //The Todo App View
     Todos.CreateTodoView = SC.TextField.extend({
         insertNewline: function() {
             var value = this.get('value');
             if (value) {
                 Todos.todoListController.createTodo(value);
                 this.set('value', '');
             }
         }
     });

     //The 'completed' View
     Todos.MarkDoneView = SC.Checkbox.extend({
         titleBinding: '.parentView.content.title', valueBinding: '.parentView.content.isDone'
     });


Friday, 3 June 2011
SproutCore Controller

         //Our Todo Controller (for the todo list)
         Todos.todoListController = SC.ArrayController.create({
             // Initialize the array controller with an empty array.
             content: [],
             // Creates a new todo with the passed title, then adds it to the array
             createTodo: function(title) {
                 var todo = Todos.Todo.create({
                     title: title
                 });
                 this.pushObject(todo);
             }
         });

         SC.ready(function() {
             Todos.mainPane = SC.TemplatePane.append({
                 layerId: 'todos',
                 templateName: 'todos'
             });
         });




Friday, 3 June 2011
MobileMe




Friday, 3 June 2011
MVVM (Model View-ViewModel) pattern


                      • Architectural pattern based on the
                        Presentation Model pattern
                      • Influenced by MVC but targeted at User-
                        interface developers specifically
                      • ViewModel responsible for exposing data
                        from Models, but considered more Model
                        than View.


Friday, 3 June 2011
KnockoutJS
                                                           Lead: Steve Sanderson




                      • Knockout catered to those using JS for
                        user interfaces, perfect for MVVM usage.
                      • Provides elegant dependency management,
                        templating and works well with jQuery.
                      • Uses Observables (variation of pub/sub)
                      • May require a learning curve to get around
                        the heavy use of data-binding.


Friday, 3 June 2011
Knockout Example
     //A Simple Todo Application

     <!--Markup with inline data-binding (Your View)-->
     <form data-bind="submit: addItem">
         New todo: <input data-bind='value: itemToAdd, valueUpdate: "afterkeydown"' />
         <button type="submit" data-bind="enable: itemToAdd().length > 0">Add</button>
         <select multiple="multiple" width="50" data-bind="options: items"> </select>
     </form>

     //JavaScript: Model
     var viewModel = {};
     viewModel.items = ko.observableArray(["Get Groceries", "Wash Car"]);
     viewModel.itemToAdd = ko.observable("");
     viewModel.addItem = function () {
         if (viewModel.itemToAdd() != "") {
             //Adds the todo item. Modifying the "items" observableArray causes any
            //associated UI components to be updated.
             viewModel.items.push(viewModel.itemToAdd());
            // Clears the text box as it's bound to the "itemToAdd" observable
             viewModel.itemToAdd("");
         }
     }

     ko.applyBindings(viewModel);
Friday, 3 June 2011
Backbone? Spine? I’m worried..




Friday, 3 June 2011
Don’t be! They’re easier to use
                         than you think.


                                Look, it’s a freakin rainbow!




Friday, 3 June 2011
How about patterns for your
                        implementation code?




Friday, 3 June 2011
Code Design Patterns


                      Creational        Behavioural               Structural


                                                                   Ease design by
                 Deal with object        Identify common
                                                                 identifying ways to
               creation mechanisms        communication
                                                                realise relationships
                  (eg. singleton)    patterns between objects
                                                                  between entities
                                           (eg. pub/sub)
                                                                     (eg. facade)




Friday, 3 June 2011
What’s commonly used?

                      • Module pattern
                      • Observer (pub/sub) pattern
                      • Inheritance pattern
                      • Prototype pattern
                      • Sandbox pattern

Friday, 3 June 2011
Module Pattern
                      • Allows us to have particular methods &
                        variables which are only accessible from
                        within the module
                      • In JavaScript, it’s used to emulate the
                        concept of classes so we can include public/
                        private methods and variables inside a
                        single object
                      • Shields functionality from the global scope
Friday, 3 June 2011
Example
      The counter variable is actually fully shielded from our global scope so it acts just like a private variable would - its existence is
       limited to within the module's closure. We can call incrementCounter() or resetCounter() but can’t directly access counter

      var testModule = (function(){
          var counter = 0;
          return {
              incrementCounter: function() {
                  return counter++;
              },
              resetCounter: function() {
                  console.log('counter value prior to reset:' + counter);
                  counter = 0
              }
          }
      })();

      /*test*/
      testModule.incrementCounter();
      testModule.resetCounter();



Friday, 3 June 2011
Pub/Sub (Observer) Pattern

                      •   General idea is the promotion of loose coupling

                      •   Objects subscribe to a specific task or activity of
                          another object and are notified when it occurs

                      •   Subscribers are observers and we refer to the
                          object being observed as the publisher

                      •   Publishers notify subscribers when events occur.




Friday, 3 June 2011
jQuery Pub/Sub Example


            $.subscribe( "/search/query", function( term, mode, order ) {
              console.log( term + mode + order );
              //perform a search request
            })

            $.publish( "/search/query", [ "cats", "images", "recent" ] );
            // logs: 'cats images recent'

            //unsubscribe handler
            $.unsubscribe( "/search/query", handle );


             * uses Ben Alman’s jQuery pub/sub implementation



Friday, 3 June 2011
Pub/Sub implementations

                      •   AmplifyJS

                      •   PubSubJS

                      •   Ben Alman’s implementation

                      •   Peter Higgin’s version

                      •   jsSignals

                      •   OpenAjaxHub

                      •   jQuery custom events *


Friday, 3 June 2011
For more implementation
                      patterns read EJDP - it’s free!

                                    • Available at
                                      addyosmani.com
                                    • Downloaded over
                                      103,000 since release
                                    • Compact - Less than
                                      50 pages.



Friday, 3 June 2011
It’s Tool Time!




Friday, 3 June 2011
Tools & Micro-frameworks

                      • Routing / Browser state management
                      • Cross-browser storage
                      • Client-side templating
                      • Feature detection
                      • Widgets/Components
                      • Dependancy management
Friday, 3 June 2011
Browser State Management
                                    The problem

                      • Bookmarkable URLs traditionally only
                        possible with a hard page-refresh
                      • For modern apps we want to preserve this
                        without any refresh involved
                      • When the URL changes application state
                        needs to as well and visa versa.
                      • Users want unique URLs that can be shared
Friday, 3 June 2011
Browser State Management
                                      The solution

                      • Solution is to change the URLs
                        location.hash (eg. http://twitter.com/#!/jquery)
                      • Lots of tools that can assist in adding
                        manageable routing to your applications via
                        this technique
                      • Make URLs SEO-friendly via the Google
                        AJAX Crawlable specs


Friday, 3 June 2011
Routing Options

                      • History.js
                      • RouteMap
                      • jQuery BBQ and jQuery.hashchange
                      • jQuery Routes
                      • Even an @140bytes version by @jed

Friday, 3 June 2011
jQuery.BBQ                            Lead: Ben Alman




           $("a").click(function(e){
               e.preventDefault();
               var href = $(this).attr( "href" );
               // Push this URL "state" onto the history hash.
               $.bbq.pushState({ url: href });
             });

           $(window).bind( "hashchange", function(e) {
               var url = $.bbq.getState( "url" );
               //additional logic depending on state
           });

           $(window).trigger( "hashchange" );



Friday, 3 June 2011
RouteMap
         var routes = window.RouteMap, rules, rule;
         $(window).bind('hashchange', routes.handler);

         rules = {
                 load_main:      {route: '/',          method: 'load'},
                 load_item:      {route: '/item/:id',  method: 'load_item'}
             };

         for (rule in rules){
             if (rules.hasOwnProperty(rule)){
                 routes.add(rule);
             }
         }




Friday, 3 June 2011
jQuery Routes

                $.routes({
                  “/”:        “Index#home”,
                  “/contact”: “Index#contact”,
                  “/about/(:page_name)”: function(params){
                  $(‘.about’).slideDown();
                 }
                });




Friday, 3 June 2011
Cross-browser Persistent Storage



                      • Store.js(localStorage, globalStorage, userData)


                      • AmplifyJS  (localStorage, sessionStorage, globalStorage, userData)


                      • PersistJS(local, session, global, userData, gears)


                      • jQuery Offline plugin

Friday, 3 June 2011
Store.js Example
       //store key/value pairs
       store.set('country','new zealand');

       //get the value of the 'country' entry
       store.get('country');

       //remove the 'country' entry
       store.remove('country');

       //clear all of the keys
       store.clear();

       //storing object literals
       store.set('profile', { username: 'johnsmith', website:'http://
       johnsmith.com'});

       //we can then get the stored object values
       var userProfile = store.get('profile');
       console.log(userProfile.username + ' has a website as ' +
       userProfile.website);

Friday, 3 June 2011
Client-side Templating
                      • Mark-up with expressions where the
                        template can be applied to data objects or
                        arrays and is rendered into the DOM.
                      • More readable than traditional string
                        concatenation
                      • Make it significantly cleaner to define a
                        'template' for data you are outputting to
                        the browser


Friday, 3 June 2011
Benefits

                      • Rendered and cached without an HTTP
                        request to the server - very fast
                      • Separation of concerns
                      • More intelligent layouts supporting
                        conditions, variables and more.



Friday, 3 June 2011
Templating Options
                      • Mustache.js
                      • Handlebars.js
                      • Dust.js
                      • jQuery.tmpl plugin
                      • Underscore Micro-templating
                      • PURE
Friday, 3 June 2011
jQuery.tmpl + pub/sub example
            <script id="userTemplate" type="text/x-jquery-tmpl">
                <li>${user}</li>
            </script>

            <script type="text/javascript">
            (function($) {
            var userList  = [];
                $.subscribe( "/new/user", function(userName){
                    if(userName.length){
                        userList.push({user: userName});
                        $( "#userTemplate" ).tmpl( userList ).appendTo
            ( "#users" );
                   }
                });
                $('#add').bind('click', function(){
                    var strUser    = $("#username").val();
                    $.publish('/new/user', strUser );
                });
            })(jQuery);
            </script>

Friday, 3 June 2011
Feature Detection
          Libraries that help detect compatibility for emerging
                    web technologies (HTML5, CSS3)


                      • Modernizr + yepnope
                      • has.js
                      • Head.JS

Friday, 3 June 2011
Modernizr
                                                               Lead: Paul Irish




                      • Detects browser support for CSS3 features
                        like @font-face, border-radius and HTML5
                        features like audio, video etc.
                      • Easily test for feature support via JavaScript
                      • Adds CSS classes for each feature detected
                        so you can style based on support as well
                      • Widely used in the industry
Friday, 3 June 2011
Modernizr <video> test
                 if (Modernizr.video && Modernizr.video.ogg){
                        // preload ogg video assets
                 }else{
                       //  load flash fallback
                 }




Friday, 3 June 2011
has.js equivalent
        (function(has, addtest, cssprop){

            var video = document.createElement("video");

            addtest("video", function(){
                return has.isHostType(video, "canPlayType");
            });

            addtest("video-ogg-theora", function(){
                return has("video") &&
                  video.canPlayType('video/ogg; codecs="theora, vorbis"');
            });

        })(has, has.add, has.cssprop);

        if(has("video-ogg-theora")){
           // load ogg assets
        }else{
          // load flash fallback
        }

Friday, 3 June 2011
yepnope.js                      Leads: Alex Sexton, Ralph Holzmann




              // conditional polyfill loading (readup on Prefixes too!)
              yepnope({
                test: Modernizr.geolocation,
                yep: 'geolocation.js',
                nope: ['geolocation-polyfill.js'],
                callback: function (url, result, key) {
                }
              });


              // if the ie version matches, load the patch
              yepnope({
                load: ['regularapp.js', 'ie7!ie8!patch.js']
              });


Friday, 3 June 2011
Widgets/UI Components

                      • jQuery UI                    (grid, spinner, menus coming)


                      • Wijmo (open + commercial widgets)
                      • jQuery Tools                       (not updated as regularly)


                      • KnockoutUI
                      • NinjaUI                (new, not as wide a collection as the others)


                      • Roll your own
                      If you’re after templated widgets it might be worth looking at dojo as it addresses this quite well



Friday, 3 June 2011
Dependancy Management

                      • JS files loaded with the script tag can be
                        blocking in nature
                      • Few modern browsers support reliable
                        parallel loading
                      • For apps requiring many files, there’s no
                        easy way to manage dependancies



Friday, 3 June 2011
Script Loader Benefits

                      • Very useful for loading scripts in a specific
                        order or dynamically depending on need
                      • Can ensure script dependancies have
                        loaded before executing behaviour that
                        relies on it being present
                      • Some can assist with more (eg. structure,
                        conditional polyfill loading)


Friday, 3 June 2011
Script Loader Options

                      • LAB.js(Kyle Simpson)



                      • RequireJS    (James Burke)



                      • StealJS
                              (Justin Meyer)



                      • ControlJS    (Steve Souders)



                      • yepnope.js     (conditional polyfill loading)


                      • HeadJS
Friday, 3 June 2011
LabJS
                                                              Lead: Kyle Simpson




                      • Works best where scripts just need to be
                        efficiently loaded in a particular order
                      • More lightweight than RequireJS
                      • It’s a mature solution that also includes
                        options for loading local scripts via Ajax,
                        preserving specific order and more.



Friday, 3 June 2011
Example
            <script>
               $LAB
               .setOptions({AlwaysPreserveOrder:true}) //optional
               .script("jquery-min.js").wait()
               .script("jquery.plugin.js")
               .wait(function(){
                  $(function(){
                   myplugin.init();
                  });
               });
            </script>




Friday, 3 June 2011
RequireJS
                                                                   Lead: James Burke




                      •   Recommend for modular code

                      •   Modules attempt to limit their impact on the
                          global namespace and be more explicit about
                          mentioning their immediate dependencies.

                      •   RequireJS also comes with an optimization tool
                          that allows you to combine and group your scripts
                          into a smaller set of minified scripts that load
                          quickly **



Friday, 3 June 2011
Example 1: Basic Usage
          <script data-main="scripts/main" src="scripts/require-
          jquery.js"></script>
          <script>

          //main.js (/main above) contains our require calls
          require(["jquery", "jquery.thickbox","jquery.tmpl"], function($) {
              //plugins loaded, execute additional behaviour
              $(function() {
                  $('.items’).doStuff();
              });
          });

          </script>




Friday, 3 June 2011
Example 2: Modules
          //modules are well-defined objects that avoid polluting the global namespace.
          //my/boxers.js has the dependancies shoppingcart.js and stockcheck.js

          define(["./shoppingcart", "./stockcheck"], function(shoppingcart, stockcheck)
          {
                  //return an object to define the "my/boxers" module.
                  return {
                      color: "pink",
                      pattern: “plaid”,
                      size: "extra-large",
                      addToCart: function() {
                           inventory.decrement(this);
                           cart.add(this);
                      }
                  }
              }
          );




Friday, 3 June 2011
Testing? If you’re not sure about
            something, rub it against a piece of paper.




Friday, 3 June 2011
Testing

                      • Unit testing
                      • Ajax, Browser and coverage testing
                      • Scripting environment


Friday, 3 June 2011
Unit Testing
                      • Essential for automated testing of any
                        serious web application.
                      • Manual functional testing is great from a
                        user-interface perspective
                      • But..unit testing allows stress-testing to
                        discover if all the inner-workings of it
                        perform as intended.


Friday, 3 June 2011
Unit Testing options
                      • QUnit
                      • Jasmine (BDD)
                      • FuncUnit
                      • Crosscheck
                      • JSSpec (BDD)
                      • jsTestDriver
Friday, 3 June 2011
QUnit Example
        //For below, remember to include markup for your QUnit test output template along
        with jquery, qunit.js and qunit.css as these are required.

        $(function(){
          test("A very basic test", function() {
            ok( true, "this test passes fine" );
            var value = "hello world";
            equals( "hello world", value, "We expect value to be hello world" );
          });

           module("Module A");
           test("first test within a module", function() {
             ok( true, "all pass" );
           });

          module("Module B");
          test("Another test", function() {
            expect(2);
            equals( true, false, "failing test" );
            equals( true, true, "passing test" );
          });
        });


Friday, 3 June 2011
Ajax, Browser and Coverage testing

                      • MockJax - intercept and simulate ajax
                        requests with a minimal impact on changes
                        to production code
                      • WebDriver + Watir + jsTestDriver -
                        automated browser testing
                      • JSCoverage - useful for discovering areas of
                        your code which may either be broken or
                        simply not being correctly


Friday, 3 June 2011
Scripting Environment
                      • Zombie.js - lightweight framework for
                        testing client-side JavaScript code in a
                        simulated environment
                      • Envjs - provides a JS implementation of the
                        browser as a usable scripting environment
                      • Bumblebee - combines Rhino, JSpec, Envjs
                        and Ant to provide an "out of the box"
                        JavaScript testing toolkit


Friday, 3 June 2011
Pack it all up!


                                 Forest does not
                                    approve.




Friday, 3 June 2011
Build Process

                      • Quick intro to Ant
                      • Concatenation
                      • Minification


Friday, 3 June 2011
Ant
                      • Excellent Java library that assists with
                        defining build files and targets
                      • Supports adding in many ready-made task-
                        types (easily loop through directories, define
                        filters, concatenate etc.)
                      • Can be used with Google’s Closure
                        compiler, YUI compressor and others.
                      • Used by many open-source projects
Friday, 3 June 2011
Ant: JSLint/Hint Validation

            ! <target depends="-init" name="-js.lint">
            !     <pathconvert pathsep=" " property="jsfiles">
            !         <fileset dir="${build.dir}/js/">
            !             <include name="*.js"/>
            !         </fileset>
            !     </pathconvert>
            !     <exec dir="${build.dir}/js/" executable="java"
            failonerror="true">
            !     <arg line="-jar ${js.jar} ${jslint.js} ${jsfiles}"/>
            !     </exec>
            !     <echo>Finished</echo>
            ! </target>




Friday, 3 June 2011
Ant: Concatenation
          <target name="-js.concatenate" depends="-js.lint"
                  description="Concatenates specified JavaScript files">
                  <concat destfile="${build.dir}/js/concat.js">
                      <fileset
                          dir="${src.js.dir}"
                          includes="*.js"
                          excludes="first.js, second.js"/>
                  </concat>
                  <echo>Finished</echo>
          </target>




Friday, 3 June 2011
Ant: Minification
      <target name="-js.minify" depends="-js.concatenate"
             description="Minifies JavaScript files">
             <apply executable="java" parallel="false" dest="${build.dir}/js">
                 <fileset
                      dir="${build.dir}/js"
                      includes="concat.js"/>
                 <arg line="-jar"/>
                 <arg path="${yui.dir}"/>
                 <srcfile/>
                 <arg line="-o"/>
                 <mapper type="glob" from="*.js" to="*-min.js"/>
                 <targetfile/>
             </apply>
             <echo>Finished</echo>
     </target>




Friday, 3 June 2011
Reminder

                      • Check out the HTML5Boilerplate build file
                      • It’s awesome and is a perfect example of
                        how a front-end project should be built
                      • Find it on GitHub https://github.com/paulirish/
                        html5-boilerplate/wiki/Build-script




Friday, 3 June 2011
Concatenation
                      • Web applications typically use a number of
                        JavaScript files (app, libraries, plugins etc)
                      • Concatenation provides you a single file
                        which can then be minified
                      • Scripts can be concatenated in any order
                      • Ant supports I/O operations like appending
                        arbitrary files (eg. licenses) and string
                        replacement as well


Friday, 3 June 2011
Concatenation Options
                      • Jake
                      • Sprockets
                      • Closure Compiler
                      • Smasher
                      • YUI Compressor
                      • Minify
Friday, 3 June 2011
Minification
                      • It’s a critical part of the build process for
                        any large JavaScript application.
                      • Every extra byte counts, which is why you
                        should ideally minify your code rather than
                        just including the unminified version
                      • The less time it takes to load, the quicker
                        your page will be ready to use


Friday, 3 June 2011
Minification Options
                      • Closure Compiler
                      • YUI Compressor
                      • UglifyJS *
                      • JSMin
                      • Microsoft Minifier
                      • Dojo’s ShrinkSafe
Friday, 3 June 2011
And that’s it!
                               It’s no longer
                                     BIG.
                                    Gettit?




Friday, 3 June 2011
Conclusions
                      • jQuery is awesome
                      • These tools can help make your
                        development process significantly easier
                      • Most are well-tested, quick wins to use.
                      • Spend more time on the project, less on
                        architecture.


Friday, 3 June 2011
For more on me


                      • @addyosmani on Twitter or GitHub
                      • http://addyosmani.com for JS + jQuery
                        articles, screencasts and resources




Friday, 3 June 2011
It’s the final slide!




Friday, 3 June 2011

More Related Content

Viewers also liked

JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureSimon Willison
 
markeeting plan Circle the power of 1
markeeting plan Circle the power of 1 markeeting plan Circle the power of 1
markeeting plan Circle the power of 1 circle power
 
Java vs javascript (XPages)
Java vs javascript (XPages)Java vs javascript (XPages)
Java vs javascript (XPages)Andrew Barickman
 
JavaOne 2014: Java vs JavaScript
JavaOne 2014:   Java vs JavaScriptJavaOne 2014:   Java vs JavaScript
JavaOne 2014: Java vs JavaScriptChris Bailey
 
Trustparency Mobile Architecture
Trustparency  Mobile ArchitectureTrustparency  Mobile Architecture
Trustparency Mobile Architecturetrustparency
 
Java vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris BaileyJava vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris BaileyJAXLondon_Conference
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsPiyush Katariya
 
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan KrausHTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan KrausWomen in Technology Poland
 
Choosing Javascript Libraries to Adopt for Development
Choosing Javascript Libraries to Adopt for DevelopmentChoosing Javascript Libraries to Adopt for Development
Choosing Javascript Libraries to Adopt for DevelopmentEdward Apostol
 
10 Building Blocks for Enterprise JavaScript
10 Building Blocks for Enterprise JavaScript10 Building Blocks for Enterprise JavaScript
10 Building Blocks for Enterprise JavaScriptGeertjan Wielenga
 
Symbol for drawing flowchart
Symbol for drawing flowchartSymbol for drawing flowchart
Symbol for drawing flowchartKamal Tamang
 
Webinar AWS 201 Delivering apps without servers
Webinar AWS 201 Delivering apps without serversWebinar AWS 201 Delivering apps without servers
Webinar AWS 201 Delivering apps without serversAmazon Web Services
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web FormsIlio Catallo
 
Testing Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsTesting Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsSam Brannen
 
Struts 2 + Spring
Struts 2 + SpringStruts 2 + Spring
Struts 2 + SpringBryan Hsueh
 
Compiler vs interpreter
Compiler vs interpreterCompiler vs interpreter
Compiler vs interpreterKamal Tamang
 
Authentication and Authorization
Authentication and AuthorizationAuthentication and Authorization
Authentication and AuthorizationTechMaster Vietnam
 

Viewers also liked (20)

JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big Picture
 
markeeting plan Circle the power of 1
markeeting plan Circle the power of 1 markeeting plan Circle the power of 1
markeeting plan Circle the power of 1
 
Java vs javascript (XPages)
Java vs javascript (XPages)Java vs javascript (XPages)
Java vs javascript (XPages)
 
JavaOne 2014: Java vs JavaScript
JavaOne 2014:   Java vs JavaScriptJavaOne 2014:   Java vs JavaScript
JavaOne 2014: Java vs JavaScript
 
Javascript libraries
Javascript librariesJavascript libraries
Javascript libraries
 
Trustparency Mobile Architecture
Trustparency  Mobile ArchitectureTrustparency  Mobile Architecture
Trustparency Mobile Architecture
 
Java vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris BaileyJava vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris Bailey
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise Applications
 
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan KrausHTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
 
Choosing Javascript Libraries to Adopt for Development
Choosing Javascript Libraries to Adopt for DevelopmentChoosing Javascript Libraries to Adopt for Development
Choosing Javascript Libraries to Adopt for Development
 
marketing and selling
marketing and sellingmarketing and selling
marketing and selling
 
10 Building Blocks for Enterprise JavaScript
10 Building Blocks for Enterprise JavaScript10 Building Blocks for Enterprise JavaScript
10 Building Blocks for Enterprise JavaScript
 
Symbol for drawing flowchart
Symbol for drawing flowchartSymbol for drawing flowchart
Symbol for drawing flowchart
 
Webinar AWS 201 Delivering apps without servers
Webinar AWS 201 Delivering apps without serversWebinar AWS 201 Delivering apps without servers
Webinar AWS 201 Delivering apps without servers
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web Forms
 
Testing Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsTesting Spring MVC and REST Web Applications
Testing Spring MVC and REST Web Applications
 
Struts 2 + Spring
Struts 2 + SpringStruts 2 + Spring
Struts 2 + Spring
 
Compiler vs interpreter
Compiler vs interpreterCompiler vs interpreter
Compiler vs interpreter
 
Boostrap - Start Up
Boostrap - Start UpBoostrap - Start Up
Boostrap - Start Up
 
Authentication and Authorization
Authentication and AuthorizationAuthentication and Authorization
Authentication and Authorization
 

Similar to Tools For jQuery Application Architecture (Extended Slides)

Javascript framework and backbone
Javascript framework and backboneJavascript framework and backbone
Javascript framework and backboneDaniel Lv
 
Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts weili_at_slideshare
 
jQuery Mobile, Backbone.js, and ASP.NET MVC
jQuery Mobile, Backbone.js, and ASP.NET MVCjQuery Mobile, Backbone.js, and ASP.NET MVC
jQuery Mobile, Backbone.js, and ASP.NET MVCTroy Miles
 
Infusion for the birds
Infusion for the birdsInfusion for the birds
Infusion for the birdscolinbdclark
 
EJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionEJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionKelum Senanayake
 
AngularJS - Architecture decisions in a large project 
AngularJS - Architecture decisionsin a large project AngularJS - Architecture decisionsin a large project 
AngularJS - Architecture decisions in a large project Elad Hirsch
 
Mobile applications chapter 5
Mobile applications chapter 5Mobile applications chapter 5
Mobile applications chapter 5Akib B. Momin
 
Angular JS, A dive to concepts
Angular JS, A dive to conceptsAngular JS, A dive to concepts
Angular JS, A dive to conceptsAbhishek Sur
 
JavaOne 2011 - Going Mobile With Java Based Technologies Today
JavaOne 2011 - Going Mobile With Java Based Technologies TodayJavaOne 2011 - Going Mobile With Java Based Technologies Today
JavaOne 2011 - Going Mobile With Java Based Technologies TodayWesley Hales
 
Updating Your Website to Drupal 7
Updating Your Website to Drupal 7Updating Your Website to Drupal 7
Updating Your Website to Drupal 7Acquia
 
Full stack development using javascript what and why - ajay chandravadiya
Full stack development using javascript   what and why - ajay chandravadiyaFull stack development using javascript   what and why - ajay chandravadiya
Full stack development using javascript what and why - ajay chandravadiyaajayrcgmail
 
Agile comparison with requriement approaches
Agile comparison with requriement approachesAgile comparison with requriement approaches
Agile comparison with requriement approachesfungfung Chen
 
Detailed design
Detailed designDetailed design
Detailed designjsokohl
 
Detailed design: Nailing it Down
Detailed design: Nailing it DownDetailed design: Nailing it Down
Detailed design: Nailing it Downjsokohl
 
AvalancheProject2012
AvalancheProject2012AvalancheProject2012
AvalancheProject2012fishetra
 
Dot Net Notts Js Unit Testing at Microlise
Dot Net Notts Js Unit Testing at  MicroliseDot Net Notts Js Unit Testing at  Microlise
Dot Net Notts Js Unit Testing at MicroliseJonathan Gregory
 

Similar to Tools For jQuery Application Architecture (Extended Slides) (20)

Javascript framework and backbone
Javascript framework and backboneJavascript framework and backbone
Javascript framework and backbone
 
Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts
 
jQuery Mobile, Backbone.js, and ASP.NET MVC
jQuery Mobile, Backbone.js, and ASP.NET MVCjQuery Mobile, Backbone.js, and ASP.NET MVC
jQuery Mobile, Backbone.js, and ASP.NET MVC
 
Infusion for the birds
Infusion for the birdsInfusion for the birds
Infusion for the birds
 
EJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionEJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another Introduction
 
AngularJS - Architecture decisions in a large project 
AngularJS - Architecture decisionsin a large project AngularJS - Architecture decisionsin a large project 
AngularJS - Architecture decisions in a large project 
 
Mobile applications chapter 5
Mobile applications chapter 5Mobile applications chapter 5
Mobile applications chapter 5
 
Angular JS, A dive to concepts
Angular JS, A dive to conceptsAngular JS, A dive to concepts
Angular JS, A dive to concepts
 
JavaOne 2011 - Going Mobile With Java Based Technologies Today
JavaOne 2011 - Going Mobile With Java Based Technologies TodayJavaOne 2011 - Going Mobile With Java Based Technologies Today
JavaOne 2011 - Going Mobile With Java Based Technologies Today
 
STI Summit 2011 - Linked services
STI Summit 2011 - Linked servicesSTI Summit 2011 - Linked services
STI Summit 2011 - Linked services
 
Updating Your Website to Drupal 7
Updating Your Website to Drupal 7Updating Your Website to Drupal 7
Updating Your Website to Drupal 7
 
Friday hacks Talk
Friday hacks TalkFriday hacks Talk
Friday hacks Talk
 
Full stack development using javascript what and why - ajay chandravadiya
Full stack development using javascript   what and why - ajay chandravadiyaFull stack development using javascript   what and why - ajay chandravadiya
Full stack development using javascript what and why - ajay chandravadiya
 
Agile comparison with requriement approaches
Agile comparison with requriement approachesAgile comparison with requriement approaches
Agile comparison with requriement approaches
 
Detailed design
Detailed designDetailed design
Detailed design
 
Detailed design: Nailing it Down
Detailed design: Nailing it DownDetailed design: Nailing it Down
Detailed design: Nailing it Down
 
Bhavesh_Shukla_Resume
Bhavesh_Shukla_ResumeBhavesh_Shukla_Resume
Bhavesh_Shukla_Resume
 
MVC
MVCMVC
MVC
 
AvalancheProject2012
AvalancheProject2012AvalancheProject2012
AvalancheProject2012
 
Dot Net Notts Js Unit Testing at Microlise
Dot Net Notts Js Unit Testing at  MicroliseDot Net Notts Js Unit Testing at  Microlise
Dot Net Notts Js Unit Testing at Microlise
 

More from Addy Osmani

Open-source Mic Talks at AOL
Open-source Mic Talks at AOLOpen-source Mic Talks at AOL
Open-source Mic Talks at AOLAddy Osmani
 
Large-Scale JavaScript Development
Large-Scale JavaScript DevelopmentLarge-Scale JavaScript Development
Large-Scale JavaScript DevelopmentAddy Osmani
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design PatternsAddy Osmani
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Addy Osmani
 
jQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksjQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksAddy Osmani
 
Evaluating jQuery Learning Material
Evaluating jQuery Learning MaterialEvaluating jQuery Learning Material
Evaluating jQuery Learning MaterialAddy Osmani
 
jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)Addy Osmani
 

More from Addy Osmani (7)

Open-source Mic Talks at AOL
Open-source Mic Talks at AOLOpen-source Mic Talks at AOL
Open-source Mic Talks at AOL
 
Large-Scale JavaScript Development
Large-Scale JavaScript DevelopmentLarge-Scale JavaScript Development
Large-Scale JavaScript Development
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design Patterns
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)
 
jQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksjQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & Tricks
 
Evaluating jQuery Learning Material
Evaluating jQuery Learning MaterialEvaluating jQuery Learning Material
Evaluating jQuery Learning Material
 
jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)
 

Recently uploaded

Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sectoritnewsafrica
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 

Recently uploaded (20)

Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 

Tools For jQuery Application Architecture (Extended Slides)

  • 2. “Always walk through life as if you have something new to learn and you will” It wasn’t me Friday, 3 June 2011
  • 3. Who Am I? • Member of the jQuery /(Bugs|Docs|Learning| Front-end)/ Subteams • Software Engineer at AOL • Writer (Essential JS Design Patterns, AddyOsmani.com, Script Junkie etc.) Friday, 3 June 2011
  • 4. AOL? You guys still exist?! Yes, we do! We actually actively use many of the tools I’ll be covering today. Friday, 3 June 2011
  • 6. jQuery is great..but! • It doesn’t provide all the tools needed to build serious JavaScript web applications • It does give us easy access to DOM manipulation, ajax , animation, events etc. • But how do we fill in the gaps for everything else? Friday, 3 June 2011
  • 7. Let’s evaluate what we’re missing by building a hypothetical application Friday, 3 June 2011
  • 9. What does it do? • Enter a friend’s name to find accounts that match it on other social networks • Allows sharing different views of results easily • Remembers user’s last known configuration Jon Stewart Friday, 3 June 2011
  • 10. btw fool..it also has to.. • Be quick and support easily changing behaviour later • Offer multiple views of the data, but require no hard page refresh • Run without any database on our end • Be well tested & perform optimally Mr. T, Project manager Friday, 3 June 2011
  • 11. Some implementation notes • Could be a single-page application • Separate application concerns & keep the code decoupled • Store persistent configuration information • Make best use of modern technologies to improve the user experience • Work cross-browser with fallbacks for IE Friday, 3 June 2011
  • 12. What do we have to work with? Friday, 3 June 2011
  • 13. jQuery can give us this Third-party API server Ajax DOM Manipulation UI Cookies (via plugin) Friday, 3 June 2011
  • 14. But we’re missing this Browser State Unit Testing Architecture Management patterns (MVC) Cross-browser persistent storage HTML5/ CSS3 Application logic Feature Detection Build Process & Optimization Client-side Communication Templating * Dependancy patterns management Widgets (pub/sub) Friday, 3 June 2011
  • 15. Let’s fill in the gaps, bro! Friday, 3 June 2011
  • 16. Code Organization: Design Patterns • Reusable solutions to commonly occurring problems in software design • Proven approaches that reflect experience and insights of developers over time • Highly flexible & can be adapted to our own needs easily Friday, 3 June 2011
  • 17. I can haz architecture? • Architecture patterns describe patterns for broader systems with a larger scope. • Certain patterns can be applied to both the server-side and client-side • Let’s look at one you know Friday, 3 June 2011
  • 18. The MVC Pattern • Separates objects into three main concerns: models, views and controllers • Traditionally heavily used on the server-side • Excellent for code-organisation in general • Implementations can vary quite significantly Friday, 3 June 2011
  • 19. Models,Views & Controllers • Models represent knowledge and data (eg. get and set methods). Isolated from Views and Controllers • Views can be considered the UI. Generally ‘dumb’. Don’t need to perform validation logic • Controller sits between Models and Views. Performs business logic, data manipulation Friday, 3 June 2011
  • 20. MVC Options • JavaScriptMVC • Backbone.js + Underscore • Spine.js • SproutCore • Sammy.js Friday, 3 June 2011
  • 21. JavaScriptMVC Lead: Justin Meyer • JMVC can really be considered two things: • Repeatable MVC architecture • All-in-one integrated development tools • Benefit is that it provides a clear path to adding functionality • Does a lot of the things you should be doing in your project anyway Friday, 3 June 2011
  • 22. JMVC Components • JMVC is broken down into 4 projects: • jQueryMVC - MVC extensions for jQuery • StealJS - Dependency management, build process, code generators • FuncUnit - A web testing framework • DocumentJS - A JavaScript docs framework Friday, 3 June 2011
  • 23. JMVC’s MVC • Model: classical model layer. organize Ajax requests and service data Effectively, a way to package and • Controller: jQuery widget factory • View: client side templating (eg. jQuery.tmpl, Micro) • Class: a JavaScript-compatible class system Friday, 3 June 2011
  • 24. JMVC Class $.Class("Speaker",{     talk: function(){]         console.log("I just said something");     } }); //usage var douglasCrockford = new Speaker(); douglasCrockford.talk(); //we can take this further as follows: Speaker("Organizer",{     announce: function(){         console.log("please take your seats");     } }); //usage var johnAlsopp = new Organizer; johnAlsopp.announce(); johnAlsopp.talk(); Friday, 3 June 2011
  • 25. JMVC Model $.Class("Model",{ //get a speaker's slides getSlides:function(eventName, success){ $.get('/slidesApi/'+ eventName, this.callback(function(q){ success(new this(q)); }) },'json') } },{ init:function(q){ $.extend(this, q) } }); Model("Crockford", { getRecentSlidesUrl: function(){ return "My most recent slides are at:" + this.slideUrl; } }); Crockford.getSlides('webDirections', function(crock){ console.log(crock.getRecentSlidesUrl()); }); Friday, 3 June 2011
  • 26. JMVC Controller $.Controller('ModalWindow',{ //sets up the widget init: function(){ this.find('.modal').hide(); }, ".newModal keyup": function(el, ev){ if(ev.keyCode == 13){ new Modal({ title : el.attr('title'), href: el.attr('href'), complete : false }).save(this.callback('created')); } }, ".modal .close click" : function(el){ el.closest('.modal').model().destroy(); } }); Friday, 3 June 2011
  • 27. JMVC View //Views essentially use client-side templating //Here's a simple example <script type='text/ejs' id='slidesEJS'> <% for(var i=0; i < slides.length; i++){ %>     <li><%=slides[i].name %> were talked about at <%=slides [i].eventName %>. A preview of the talk can be found below:<img src="<%=slides [i].thumbnailUrl %>"/></li> <%} %> </script> //Which can be easily rendered using.. $("#slides").html('slidesEJS', slideData); Friday, 3 June 2011
  • 28. Why JMVC? • Maturity & includes testing, dependency management, build tools, client side templates. • Perfect for large applications where an all- in-one solution is required. • Scales well. It was most recently used by Grooveshark in their application re-write. Friday, 3 June 2011
  • 30. Backbone.js Lead: Jeremy Ashkenas • Backbone provides the ‘backbone’ you need to organize your code using the MVC pattern. • Excellent for a lightweight solution where you select the additional components you feel work best for your project. • Works best for SPAs (single-page apps) Friday, 3 June 2011
  • 31. Who uses it? MetaLab SoundCloud BitTorrent Friday, 3 June 2011
  • 32. Backbone’s MVC • Models: represent data that can be created, validated, destroyed & listened to for changes. Collections are sets of models. • Views: display the model’s data / provide the user interface layer • Controller: methods for routing client-side URL fragments Friday, 3 June 2011
  • 33. Backbone Model window.Note = Backbone.Model.extend({ // Default attributes for the note defaults: { content: "empty note", done: false }, // Make sure each note has default 'content' initialize: function() { if (!this.get("content")) { this.set({"content": this.defaults.content}); } }, // Toggle the `done` state of this note toggle: function() { this.save({done: !this.get("done")}); }, // Remove this note from local-storage. clear: function() { this.destroy(); this.view.remove(); } }); Friday, 3 June 2011
  • 34. Backbone Collection window.StickyNotes = Backbone.Collection.extend({ // Reference to this collection's model. model: Note, // Save all of the note items under the `"notes"` namespace. localStorage: new Store("notes"), // Filter down the list of all notes that are finished. done: function() { return this.filter(function(note){ return note.get('done'); }); }, // Filter down the list to only note items that are still not finished. remaining: function() { return this.without.apply(this, this.done()); } }); Friday, 3 June 2011
  • 35. Backbone View window.AppView = Backbone.View.extend({ el: $("#notesapp"), events: { "keypress #new-note": "createOnEnter", "click .note-clear a": "clearCompleted" }, initialize: function() { _.bindAll(this, 'addOne', 'addAll', 'render'); this.input = this.$("#new-note"); StickyNotes.bind('add', this.addOne); StickyNotes.bind('reset', this.addAll); StickyNotes.bind('all', this.render); StickyNotes.fetch(); }, render: function() { this.$('#notes-info').html({ /* template logic could go here*/ }); }, addOne:function(note){ //add a single note var view = new NoteView({model: todo}); this.$("#note-list").append(view.render().el); } addAll: function() { //add everything to the stickynotes collection at once StickyNotes.each(this.addOne); }, }); Friday, 3 June 2011
  • 36. Spine.js Lead: Alex MacCaw • A lightweight MVC framework with a focus on providing an inheritance model through classes and extension. • Based on Backbone's API so developers may find getting started a little easier than expected • Personally found it a nice alternative Friday, 3 June 2011
  • 37. Spine’s MVC • Classes: traditional ‘classes’ powered by an emulated version of Object.create • Models: your application's data storage models as well as any logic associated with this data. Based on classes. • Controllers: Similar to ‘Views’ - each controller has an element associated with it. Also based on classes. Friday, 3 June 2011
  • 38. Spine vs. Backbone • Spine’s Controllers are effectively the same concept as Backbone's Views. • In terms of inheritance, Backbone uses constructor functions and prototypes whilst Spine uses an emulated version of Object.create for a simulated class system • Spine doesn’t require underscore as a dependancy Friday, 3 June 2011
  • 39. Sammy.js Lead: Aaron Quint • A popular lightweight framework that allows you to easily define route based applications. • Some consider it the best controller framework but it doesn't provide the Model and View aspects itself. • Good for SPAs requiring code organization. • Also used by apps such as PaperlessPost Friday, 3 June 2011
  • 40. Sammy.js Example //initialize the application var app = $.sammy(function() { //set the plugin for providing create/render client side templates this.use(Sammy.Template); //handler for a page that doesn’t exist or can’t be located this.notFound = function(verb, path) { this.runRoute('get', '#/404'); }; //define a route for 'about' this.get('#/about', function() { //partial calls template() because of the file extension this.partial('templates/about.template', {}, function(html) { $('#page').html(html); }); }); //define a route for '404' pages this.get('#/404', function() { this.partial('templates/404.template', {}, function(html) { $('#page').html(html); }); }); }); Friday, 3 June 2011
  • 42. SproutCore Lead: Charles Jolley • Recommended for applications wishing to have a desktop-like ‘richness’. • Data binding framework makes updating views easy • Extends MVC to include a server interface, a display that 'paints' your interface and responders for controlling application state. • Often used for larger enterprise apps, but may be a little overkill for smaller ones. • Apple used SproutCore for MobileMe Friday, 3 June 2011
  • 43. SproutCore Class + View //Root object for the application. SC.Application is usually needed if you //wish to use SC.Responder later to route events. Todos = SC.Application.create(); //SC.Object is the root class for most classes defined by SproutCore. Todos.Todo = SC.Object.extend({ title: null, isDone: false }); //The Todo App View Todos.CreateTodoView = SC.TextField.extend({ insertNewline: function() { var value = this.get('value'); if (value) { Todos.todoListController.createTodo(value); this.set('value', ''); } } }); //The 'completed' View Todos.MarkDoneView = SC.Checkbox.extend({ titleBinding: '.parentView.content.title', valueBinding: '.parentView.content.isDone' }); Friday, 3 June 2011
  • 44. SproutCore Controller //Our Todo Controller (for the todo list) Todos.todoListController = SC.ArrayController.create({ // Initialize the array controller with an empty array. content: [], // Creates a new todo with the passed title, then adds it to the array createTodo: function(title) { var todo = Todos.Todo.create({ title: title }); this.pushObject(todo); } }); SC.ready(function() { Todos.mainPane = SC.TemplatePane.append({ layerId: 'todos', templateName: 'todos' }); }); Friday, 3 June 2011
  • 46. MVVM (Model View-ViewModel) pattern • Architectural pattern based on the Presentation Model pattern • Influenced by MVC but targeted at User- interface developers specifically • ViewModel responsible for exposing data from Models, but considered more Model than View. Friday, 3 June 2011
  • 47. KnockoutJS Lead: Steve Sanderson • Knockout catered to those using JS for user interfaces, perfect for MVVM usage. • Provides elegant dependency management, templating and works well with jQuery. • Uses Observables (variation of pub/sub) • May require a learning curve to get around the heavy use of data-binding. Friday, 3 June 2011
  • 48. Knockout Example //A Simple Todo Application <!--Markup with inline data-binding (Your View)--> <form data-bind="submit: addItem"> New todo: <input data-bind='value: itemToAdd, valueUpdate: "afterkeydown"' /> <button type="submit" data-bind="enable: itemToAdd().length > 0">Add</button> <select multiple="multiple" width="50" data-bind="options: items"> </select> </form> //JavaScript: Model var viewModel = {}; viewModel.items = ko.observableArray(["Get Groceries", "Wash Car"]); viewModel.itemToAdd = ko.observable(""); viewModel.addItem = function () { if (viewModel.itemToAdd() != "") { //Adds the todo item. Modifying the "items" observableArray causes any //associated UI components to be updated. viewModel.items.push(viewModel.itemToAdd()); // Clears the text box as it's bound to the "itemToAdd" observable viewModel.itemToAdd(""); } } ko.applyBindings(viewModel); Friday, 3 June 2011
  • 49. Backbone? Spine? I’m worried.. Friday, 3 June 2011
  • 50. Don’t be! They’re easier to use than you think. Look, it’s a freakin rainbow! Friday, 3 June 2011
  • 51. How about patterns for your implementation code? Friday, 3 June 2011
  • 52. Code Design Patterns Creational Behavioural Structural Ease design by Deal with object Identify common identifying ways to creation mechanisms communication realise relationships (eg. singleton) patterns between objects between entities (eg. pub/sub) (eg. facade) Friday, 3 June 2011
  • 53. What’s commonly used? • Module pattern • Observer (pub/sub) pattern • Inheritance pattern • Prototype pattern • Sandbox pattern Friday, 3 June 2011
  • 54. Module Pattern • Allows us to have particular methods & variables which are only accessible from within the module • In JavaScript, it’s used to emulate the concept of classes so we can include public/ private methods and variables inside a single object • Shields functionality from the global scope Friday, 3 June 2011
  • 55. Example The counter variable is actually fully shielded from our global scope so it acts just like a private variable would - its existence is limited to within the module's closure. We can call incrementCounter() or resetCounter() but can’t directly access counter var testModule = (function(){     var counter = 0;     return {         incrementCounter: function() {             return counter++;         },         resetCounter: function() {             console.log('counter value prior to reset:' + counter);             counter = 0         }     } })(); /*test*/ testModule.incrementCounter(); testModule.resetCounter(); Friday, 3 June 2011
  • 56. Pub/Sub (Observer) Pattern • General idea is the promotion of loose coupling • Objects subscribe to a specific task or activity of another object and are notified when it occurs • Subscribers are observers and we refer to the object being observed as the publisher • Publishers notify subscribers when events occur. Friday, 3 June 2011
  • 57. jQuery Pub/Sub Example $.subscribe( "/search/query", function( term, mode, order ) {   console.log( term + mode + order );   //perform a search request }) $.publish( "/search/query", [ "cats", "images", "recent" ] ); // logs: 'cats images recent' //unsubscribe handler $.unsubscribe( "/search/query", handle ); * uses Ben Alman’s jQuery pub/sub implementation Friday, 3 June 2011
  • 58. Pub/Sub implementations • AmplifyJS • PubSubJS • Ben Alman’s implementation • Peter Higgin’s version • jsSignals • OpenAjaxHub • jQuery custom events * Friday, 3 June 2011
  • 59. For more implementation patterns read EJDP - it’s free! • Available at addyosmani.com • Downloaded over 103,000 since release • Compact - Less than 50 pages. Friday, 3 June 2011
  • 61. Tools & Micro-frameworks • Routing / Browser state management • Cross-browser storage • Client-side templating • Feature detection • Widgets/Components • Dependancy management Friday, 3 June 2011
  • 62. Browser State Management The problem • Bookmarkable URLs traditionally only possible with a hard page-refresh • For modern apps we want to preserve this without any refresh involved • When the URL changes application state needs to as well and visa versa. • Users want unique URLs that can be shared Friday, 3 June 2011
  • 63. Browser State Management The solution • Solution is to change the URLs location.hash (eg. http://twitter.com/#!/jquery) • Lots of tools that can assist in adding manageable routing to your applications via this technique • Make URLs SEO-friendly via the Google AJAX Crawlable specs Friday, 3 June 2011
  • 64. Routing Options • History.js • RouteMap • jQuery BBQ and jQuery.hashchange • jQuery Routes • Even an @140bytes version by @jed Friday, 3 June 2011
  • 65. jQuery.BBQ Lead: Ben Alman $("a").click(function(e){     e.preventDefault();     var href = $(this).attr( "href" );     // Push this URL "state" onto the history hash.     $.bbq.pushState({ url: href });   }); $(window).bind( "hashchange", function(e) {     var url = $.bbq.getState( "url" );     //additional logic depending on state }); $(window).trigger( "hashchange" ); Friday, 3 June 2011
  • 66. RouteMap var routes = window.RouteMap, rules, rule; $(window).bind('hashchange', routes.handler); rules = {         load_main:      {route: '/',          method: 'load'},         load_item:      {route: '/item/:id',  method: 'load_item'}     }; for (rule in rules){     if (rules.hasOwnProperty(rule)){         routes.add(rule);     } } Friday, 3 June 2011
  • 67. jQuery Routes $.routes({   “/”: “Index#home”,   “/contact”: “Index#contact”,   “/about/(:page_name)”: function(params){   $(‘.about’).slideDown();  } }); Friday, 3 June 2011
  • 68. Cross-browser Persistent Storage • Store.js(localStorage, globalStorage, userData) • AmplifyJS (localStorage, sessionStorage, globalStorage, userData) • PersistJS(local, session, global, userData, gears) • jQuery Offline plugin Friday, 3 June 2011
  • 69. Store.js Example //store key/value pairs store.set('country','new zealand'); //get the value of the 'country' entry store.get('country'); //remove the 'country' entry store.remove('country'); //clear all of the keys store.clear(); //storing object literals store.set('profile', { username: 'johnsmith', website:'http:// johnsmith.com'}); //we can then get the stored object values var userProfile = store.get('profile'); console.log(userProfile.username + ' has a website as ' + userProfile.website); Friday, 3 June 2011
  • 70. Client-side Templating • Mark-up with expressions where the template can be applied to data objects or arrays and is rendered into the DOM. • More readable than traditional string concatenation • Make it significantly cleaner to define a 'template' for data you are outputting to the browser Friday, 3 June 2011
  • 71. Benefits • Rendered and cached without an HTTP request to the server - very fast • Separation of concerns • More intelligent layouts supporting conditions, variables and more. Friday, 3 June 2011
  • 72. Templating Options • Mustache.js • Handlebars.js • Dust.js • jQuery.tmpl plugin • Underscore Micro-templating • PURE Friday, 3 June 2011
  • 73. jQuery.tmpl + pub/sub example <script id="userTemplate" type="text/x-jquery-tmpl">     <li>${user}</li> </script> <script type="text/javascript"> (function($) { var userList  = [];     $.subscribe( "/new/user", function(userName){         if(userName.length){             userList.push({user: userName});             $( "#userTemplate" ).tmpl( userList ).appendTo ( "#users" );        }     });     $('#add').bind('click', function(){         var strUser    = $("#username").val();         $.publish('/new/user', strUser );     }); })(jQuery); </script> Friday, 3 June 2011
  • 74. Feature Detection Libraries that help detect compatibility for emerging web technologies (HTML5, CSS3) • Modernizr + yepnope • has.js • Head.JS Friday, 3 June 2011
  • 75. Modernizr Lead: Paul Irish • Detects browser support for CSS3 features like @font-face, border-radius and HTML5 features like audio, video etc. • Easily test for feature support via JavaScript • Adds CSS classes for each feature detected so you can style based on support as well • Widely used in the industry Friday, 3 June 2011
  • 76. Modernizr <video> test if (Modernizr.video && Modernizr.video.ogg){ // preload ogg video assets }else{       //  load flash fallback } Friday, 3 June 2011
  • 77. has.js equivalent (function(has, addtest, cssprop){     var video = document.createElement("video");     addtest("video", function(){         return has.isHostType(video, "canPlayType");     });     addtest("video-ogg-theora", function(){         return has("video") && video.canPlayType('video/ogg; codecs="theora, vorbis"');     }); })(has, has.add, has.cssprop); if(has("video-ogg-theora")){    // load ogg assets }else{   // load flash fallback } Friday, 3 June 2011
  • 78. yepnope.js Leads: Alex Sexton, Ralph Holzmann // conditional polyfill loading (readup on Prefixes too!) yepnope({   test: Modernizr.geolocation,   yep: 'geolocation.js',   nope: ['geolocation-polyfill.js'],   callback: function (url, result, key) {   } }); // if the ie version matches, load the patch yepnope({ load: ['regularapp.js', 'ie7!ie8!patch.js'] }); Friday, 3 June 2011
  • 79. Widgets/UI Components • jQuery UI (grid, spinner, menus coming) • Wijmo (open + commercial widgets) • jQuery Tools (not updated as regularly) • KnockoutUI • NinjaUI (new, not as wide a collection as the others) • Roll your own If you’re after templated widgets it might be worth looking at dojo as it addresses this quite well Friday, 3 June 2011
  • 80. Dependancy Management • JS files loaded with the script tag can be blocking in nature • Few modern browsers support reliable parallel loading • For apps requiring many files, there’s no easy way to manage dependancies Friday, 3 June 2011
  • 81. Script Loader Benefits • Very useful for loading scripts in a specific order or dynamically depending on need • Can ensure script dependancies have loaded before executing behaviour that relies on it being present • Some can assist with more (eg. structure, conditional polyfill loading) Friday, 3 June 2011
  • 82. Script Loader Options • LAB.js(Kyle Simpson) • RequireJS (James Burke) • StealJS (Justin Meyer) • ControlJS (Steve Souders) • yepnope.js (conditional polyfill loading) • HeadJS Friday, 3 June 2011
  • 83. LabJS Lead: Kyle Simpson • Works best where scripts just need to be efficiently loaded in a particular order • More lightweight than RequireJS • It’s a mature solution that also includes options for loading local scripts via Ajax, preserving specific order and more. Friday, 3 June 2011
  • 84. Example <script>    $LAB    .setOptions({AlwaysPreserveOrder:true}) //optional    .script("jquery-min.js").wait()    .script("jquery.plugin.js")    .wait(function(){       $(function(){        myplugin.init();       });    }); </script> Friday, 3 June 2011
  • 85. RequireJS Lead: James Burke • Recommend for modular code • Modules attempt to limit their impact on the global namespace and be more explicit about mentioning their immediate dependencies. • RequireJS also comes with an optimization tool that allows you to combine and group your scripts into a smaller set of minified scripts that load quickly ** Friday, 3 June 2011
  • 86. Example 1: Basic Usage <script data-main="scripts/main" src="scripts/require- jquery.js"></script> <script> //main.js (/main above) contains our require calls require(["jquery", "jquery.thickbox","jquery.tmpl"], function($) {     //plugins loaded, execute additional behaviour     $(function() {         $('.items’).doStuff();     }); }); </script> Friday, 3 June 2011
  • 87. Example 2: Modules //modules are well-defined objects that avoid polluting the global namespace. //my/boxers.js has the dependancies shoppingcart.js and stockcheck.js define(["./shoppingcart", "./stockcheck"], function(shoppingcart, stockcheck) { //return an object to define the "my/boxers" module. return { color: "pink", pattern: “plaid”, size: "extra-large", addToCart: function() { inventory.decrement(this); cart.add(this); } } } ); Friday, 3 June 2011
  • 88. Testing? If you’re not sure about something, rub it against a piece of paper. Friday, 3 June 2011
  • 89. Testing • Unit testing • Ajax, Browser and coverage testing • Scripting environment Friday, 3 June 2011
  • 90. Unit Testing • Essential for automated testing of any serious web application. • Manual functional testing is great from a user-interface perspective • But..unit testing allows stress-testing to discover if all the inner-workings of it perform as intended. Friday, 3 June 2011
  • 91. Unit Testing options • QUnit • Jasmine (BDD) • FuncUnit • Crosscheck • JSSpec (BDD) • jsTestDriver Friday, 3 June 2011
  • 92. QUnit Example //For below, remember to include markup for your QUnit test output template along with jquery, qunit.js and qunit.css as these are required. $(function(){ test("A very basic test", function() { ok( true, "this test passes fine" ); var value = "hello world"; equals( "hello world", value, "We expect value to be hello world" ); }); module("Module A"); test("first test within a module", function() { ok( true, "all pass" ); }); module("Module B"); test("Another test", function() { expect(2); equals( true, false, "failing test" ); equals( true, true, "passing test" ); }); }); Friday, 3 June 2011
  • 93. Ajax, Browser and Coverage testing • MockJax - intercept and simulate ajax requests with a minimal impact on changes to production code • WebDriver + Watir + jsTestDriver - automated browser testing • JSCoverage - useful for discovering areas of your code which may either be broken or simply not being correctly Friday, 3 June 2011
  • 94. Scripting Environment • Zombie.js - lightweight framework for testing client-side JavaScript code in a simulated environment • Envjs - provides a JS implementation of the browser as a usable scripting environment • Bumblebee - combines Rhino, JSpec, Envjs and Ant to provide an "out of the box" JavaScript testing toolkit Friday, 3 June 2011
  • 95. Pack it all up! Forest does not approve. Friday, 3 June 2011
  • 96. Build Process • Quick intro to Ant • Concatenation • Minification Friday, 3 June 2011
  • 97. Ant • Excellent Java library that assists with defining build files and targets • Supports adding in many ready-made task- types (easily loop through directories, define filters, concatenate etc.) • Can be used with Google’s Closure compiler, YUI compressor and others. • Used by many open-source projects Friday, 3 June 2011
  • 98. Ant: JSLint/Hint Validation ! <target depends="-init" name="-js.lint"> ! <pathconvert pathsep=" " property="jsfiles"> ! <fileset dir="${build.dir}/js/"> ! <include name="*.js"/> ! </fileset> ! </pathconvert> ! <exec dir="${build.dir}/js/" executable="java" failonerror="true"> ! <arg line="-jar ${js.jar} ${jslint.js} ${jsfiles}"/> ! </exec> ! <echo>Finished</echo> ! </target> Friday, 3 June 2011
  • 99. Ant: Concatenation <target name="-js.concatenate" depends="-js.lint" description="Concatenates specified JavaScript files"> <concat destfile="${build.dir}/js/concat.js"> <fileset dir="${src.js.dir}" includes="*.js" excludes="first.js, second.js"/> </concat> <echo>Finished</echo> </target> Friday, 3 June 2011
  • 100. Ant: Minification <target name="-js.minify" depends="-js.concatenate" description="Minifies JavaScript files"> <apply executable="java" parallel="false" dest="${build.dir}/js"> <fileset dir="${build.dir}/js" includes="concat.js"/> <arg line="-jar"/> <arg path="${yui.dir}"/> <srcfile/> <arg line="-o"/> <mapper type="glob" from="*.js" to="*-min.js"/> <targetfile/> </apply> <echo>Finished</echo> </target> Friday, 3 June 2011
  • 101. Reminder • Check out the HTML5Boilerplate build file • It’s awesome and is a perfect example of how a front-end project should be built • Find it on GitHub https://github.com/paulirish/ html5-boilerplate/wiki/Build-script Friday, 3 June 2011
  • 102. Concatenation • Web applications typically use a number of JavaScript files (app, libraries, plugins etc) • Concatenation provides you a single file which can then be minified • Scripts can be concatenated in any order • Ant supports I/O operations like appending arbitrary files (eg. licenses) and string replacement as well Friday, 3 June 2011
  • 103. Concatenation Options • Jake • Sprockets • Closure Compiler • Smasher • YUI Compressor • Minify Friday, 3 June 2011
  • 104. Minification • It’s a critical part of the build process for any large JavaScript application. • Every extra byte counts, which is why you should ideally minify your code rather than just including the unminified version • The less time it takes to load, the quicker your page will be ready to use Friday, 3 June 2011
  • 105. Minification Options • Closure Compiler • YUI Compressor • UglifyJS * • JSMin • Microsoft Minifier • Dojo’s ShrinkSafe Friday, 3 June 2011
  • 106. And that’s it! It’s no longer BIG. Gettit? Friday, 3 June 2011
  • 107. Conclusions • jQuery is awesome • These tools can help make your development process significantly easier • Most are well-tested, quick wins to use. • Spend more time on the project, less on architecture. Friday, 3 June 2011
  • 108. For more on me • @addyosmani on Twitter or GitHub • http://addyosmani.com for JS + jQuery articles, screencasts and resources Friday, 3 June 2011
  • 109. It’s the final slide! Friday, 3 June 2011