SlideShare a Scribd company logo
Organizing Code with
                              JavaScriptMVC
                       git clone git://github.com/tdreyno/jquery-demo.git



                                      Thomas Reynolds




Sunday, October 17, 2010
Who am I?



Sunday, October 17, 2010
Who am I?
                • JavascriptMVC Core Committer

                • jQuery Plugin Developer

                • @tdreyno

                • github.com/tdreyno

                • awardwinningfjords.com


Sunday, October 17, 2010
Sunday, October 17, 2010
Sunday, October 17, 2010
Getting to Know You



Sunday, October 17, 2010
Getting to Know You

                • Traditional Website Developers

                • Web Application Developers

                • Mobile Web Developers




Sunday, October 17, 2010
Organizing Code



Sunday, October 17, 2010
Common Problems



Sunday, October 17, 2010
One BIG File



Sunday, October 17, 2010
$(function(){

                             // make external links open in new window
                             $('a[rel="external"]').attr('target','_blank');

                             if ($("body").attr('id') == 'homepage'){
                               bindHomepage();
                             } else if ($("body").hasClass('lessons_index') || $("body").hasClass('lessons_list')) {
                               bindLessonsList();
                             } else if ($("body").hasClass('documents_detail')) {
                               if (weird === 0){
                                 showDetail();
                               }
                               weird++;
                             } else if ($("body").hasClass('lessons_detail')) {
                               if (weird === 0){
                                 showDetail();
                               }
                               weird++;

                               // look for requests to launch the activity editor
                               var edit_matches = window.location.search.match(/[?&]edit_activity=(d+)&type=([^=?&]+)/)
                               if (edit_matches) {
                                 var edit_activity_id = edit_matches[1];
                                 var edit_activity_type = edit_matches[2];
                                 launchCreatorModal({
                                   id: edit_activity_id,
                                   type: edit_activity_type,
                                   mode: 'edit'
                                 });
                               }

                             } else if ($("body").attr('id')=='my_classroom'){
                                 bindMyClassroom();
                             } else if ($("body").hasClass('templates_index')){
                                 bindTemplates();
                             }

                             bindCreatorLinks();

                             // This can cause an error in Chrome, but the problem is already patched (03/2010)
                             // and awaiting a release.
                             window.onbeforeunload = handleOnBeforeUnload;
                             window.onunload = handleOnUnload;

                             if (window.location.search.match(/[?&]from_activity=1/)) {
                               $('#back_to_activity a').pulse();
                             }

                             bindSearchToggle();

                             // Load the "Star This" code
                             // Should be inlined using something like Sprockets in the future
                             $.getScript("/js/stars-and-add-to-buttons.js");

                             // Handle resources page toggles
                             $("p.more a.morelink, ul.more a.morelink, a.arrow").click(function(e) {
                               e.preventDefault();
                               var parentElem = $(this).parents(".openRow, .closedRow");
                               parentElem[0].className = parentElem.is(".openRow") ? "closedRow" : "openRow";
                             });

                             if (document.location.hash.length) {
                               $("a[name=" + document.location.hash.replace(/#/, "") + "]").parents(".closedRow").find(".arrow").click();
Sunday, October 17, 2010     }
One BIG File

                • 800+ Lines

                • Difficult to read

                • No Documentation

                • No Test Cases

                • Not DRY


Sunday, October 17, 2010
The Solution?

                     Break into Maintainable
                        Chunks of Code



Sunday, October 17, 2010
Problems with
                            Multiple Files
                • Does everything go into one folder?

                • What if something is needed in two different
                  places?

                • Communication

                • Loading speed

                • Loading order


Sunday, October 17, 2010
JavascriptMVC Provides

                           Dependency
                           Management



Sunday, October 17, 2010
StealJS
                • Asynchronously Loading

                • Provides a structure for organizing files

                • Cleans, Combines & Compresses

                • Bundles Presentational CSS

                • Supports CoffeeScript & Less.js


Sunday, October 17, 2010
StealJS Example
                            steal.plugins("jquery/controller",
                                           "jquery/event/drag",
                                           "jquery/dom/within")
                            .then(function($) {
                               // Configure JavascriptMVC plugins
                            })
                            .resources("jquerytools.tabs",
                                        "jquery.ui.position",
                                        "jquerytools.scrollable")
                            .then(function($) {
                               // Configure jQuery plugins
                            })
                            .controllers("tile",
                                          "large_zoom",
                                          "medium_zoom",
                                          "small_zoom",
                                          "control",
                                          "image")
                            .then(function($) {
                             // All dependencies are satisfied
                            })


Sunday, October 17, 2010
Plugin
                    • Basic unit of organization
                    • Simply, a folder.
                    • Initialized by a .js file named the same as
                           the app.
                           • For example: my_plugin/my_plugin.js
                    • Can include other plugins
Sunday, October 17, 2010
Resources
                • A folder named “resources/” in your plugin

                • Contains raw Javascript files, often a jQuery plugin

                     • jQuery UI

                     • jQuery Tools

                     • Modernizr

                     • Et cetera



Sunday, October 17, 2010
Controller

                • Could be called several things:

                     • View Controller

                     • Widget

                     • $.fn on Steroids




Sunday, October 17, 2010
Controller
                • Controls a DOM element

                     • Uses templates to create & manipulate
                       the DOM

                • Manages state

                • Uses PubSub (OpenAjax) to communicate

                • Responds to events


Sunday, October 17, 2010
Example Site




Sunday, October 17, 2010
Example Site




Sunday, October 17, 2010
Example Site




Sunday, October 17, 2010
Example Site
                •          Homepage
                    •       A slideshow
                    •       Tabs
                    •       Custom Autocomplete Search
                •          Contact Page
                    •       Form with Validator
                    •       “Success” lightbox
                    •       Custom Autocomplete Search


Sunday, October 17, 2010
Getting Started
                     • Download zip file from GitHub
                           • http://github.com/jupiterjs/framework/
                             downloads
                           • Unzip
                     • Install gem
                           • gem install javascriptmvc --pre
                           • jmvc-init jqconf-demo


Sunday, October 17, 2010
Fresh JavascriptMVC
                         Application




Sunday, October 17, 2010
Homepage “App”




Sunday, October 17, 2010
Homepage HTML
                           <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                                       "http://www.w3.org/TR/html4/strict.dtd">
                           <html lang="en">
                             <head>
                               <title>homepage</title>
                             </head>
                             <body>
                               <h1>Thanks for stealing StealJS!</h1>
                               <p>Don't worry, it's open source. It's only
                           stealing if you don't do something awesome with it.</p>
                               <div id='content'></div>

                               <script type='text/javascript'
                                        src='../steal/steal.js?homepage'></script>
                             </body>
                           </html>




Sunday, October 17, 2010
Homepage Javascript
                 steal( 'resources/example' )   // Loads 'resources/example.js'
                   .css( 'homepage' )           // Loads 'homepage.css'
                   .plugins(
                     'steal/less',
                     'steal/coffee' )           // Loads 'steal/less/less.js' and
                                                // 'steal/coffee/coffee.js'

                    .then(function(){           // Adds a function to be called back
                                                // once all prior files have been
                                                // loaded and run
                      steal.coffee('resources/example') // Loads 'resources/example.coffee'
                             .less('resources/example'); // Loads 'resources/example.less'
                    });




Sunday, October 17, 2010
Resources
                           (jQuery Plugins)




Sunday, October 17, 2010
homepage/homepage.js
                           steal.plugins("jquery")
                                .resources("slider", "tabs")
                                .then(function($) {

                           $(document).ready(function() {
                             $("#slider").slider();
                             $("#tabs").tabs();
                           });

                           });




Sunday, October 17, 2010
Sunday, October 17, 2010
Contact Page “App”




Sunday, October 17, 2010
Contact Page
                           steal.plugins("jquery")
                                .resources("validator", "lightbox")
                                .then(function() {

                           $("form").validator();

                           $("form").submit(function() {
                             $.lightbox();
                           });

                           });




Sunday, October 17, 2010
Autocomplete Search
                        (Plugin)



Sunday, October 17, 2010
TR.SearchControl
                                Plugin




Sunday, October 17, 2010
But Wait!?



Sunday, October 17, 2010
FuncUnit
                • Test-Driven Development:

                     • Results well thought-out code

                     • Inspires trust in your users

                     • Documents logic

                     • Provides an automated means of making
                       sure everything still works


Sunday, October 17, 2010
TR.SearchControl
                               FuncUnit
                             module("TR.SearchControl", {
                                setup: function() {
                                  $("form#search").tr_search_control();
                                }
                             })

                             test("Autocomplete", function(){
                                // Type into text box
                                // Extra results should appear
                             })

                             test("Submit", function(){
                                // Clicking submit or hitting enter
                                // Should submit the form
                             })




Sunday, October 17, 2010
TR.SearchControl
                               FuncUnit




Sunday, October 17, 2010
Search Controller
                            steal.plugins("jquery/controller")
                                 .resources("autocomplete")
                                 .then(function($) {

                            $.Controller.extend("TR.SearchControl", { }, {
                              init: function() {
                                 this.element.autocomplete();
                              },

                              "input:text change": function() {
                                 // Autocomplete lookup
                              },

                              "submit": function() {
                                // Ajax form submit
                              }
                            });

                            $(document).ready(function() {
                              $("form#search").tr_search_control();
                            });

                            });
Sunday, October 17, 2010
TR.SearchControl
                               FuncUnit




Sunday, October 17, 2010
homepage/homepage.js
                           steal.plugins("jquery")
                                .resources("slider", "tabs")
                                .plugins("tr/search_control")
                                .then(function($) {

                           $(document).ready(function() {
                             $("#slider").slider();
                             $("#tabs").tabs();
                           });

                           });




Sunday, October 17, 2010
Sunday, October 17, 2010
All Done Coding,
                           Time to Optimize



Sunday, October 17, 2010
Building & Compression
                    •      Packages
                           • 0.js - Shared between all apps
                             (jQuery, JavascriptMVC componetns, etc)
                           • homepage/production.js
                             Unique to Homepage
                           • contacts/contact.js
                             Unique to Contact
                    •      Google Closure Compiler
                           • Dojo Shrinksafe also available

Sunday, October 17, 2010
Building & Compression


                       ./steal/js steal/buildjs homepage contact




Sunday, October 17, 2010
Building & Compression




Sunday, October 17, 2010
Production HTML
                           <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                                       "http://www.w3.org/TR/html4/strict.dtd">
                           <html lang="en">
                             <head>
                               <title>homepage</title>
                             </head>
                             <body>
                               <h1>Thanks for stealing StealJS!</h1>
                               <p>Don't worry, it's open source. It's only stealing if you
                           don't do something awesome with it.</p>
                               <div id='content'></div>

                               <script type='text/javascript'
                                         src='../steal/steal.js?homepage,production'>
                               </script>
                             </body>
                           </html>




Sunday, October 17, 2010
Sunday, October 17, 2010
Summary
                • Organize code into apps & plugins

                • Always Test Your Code

                • Use consistent naming and organization

                • Create sharable modules (Github)

                • Optimize!


Sunday, October 17, 2010
JavascriptMVC Provides
                           Models (JSON API wrappers)


                           Views (EJS, Micro, Jaml)


                           Controllers (Event system, data binding)


                           Functional testing (FuncUnit, qunit, selenium)


                           Dependency management (Optimization)
Sunday, October 17, 2010
My Plugins

                     JavascriptMVC convenience gem

                     • gem install javascriptmvc --pre

                           • jmvc-init newproject

                           • jmvc-gen newapp




Sunday, October 17, 2010
My Plugins
                • steal.plugins("ss/state_machine")
                     http://github.com/secondstory/secondstoryjs-
                     statemachine

                • steal.plugins("ss/router")
                     http://github.com/secondstory/secondstoryjs-router

                • steal.plugins("tr/views/mustache")
                     http://github.com/tdreyno/mustache-javascriptmvc

                • steal.plugins("tr/controller/pure")
                     http://github.com/tdreyno/javascriptmvc-pure



Sunday, October 17, 2010
Thank you
                • Questions?

                • http://v3.javascriptmvc.com/index.html

                • @tdreyno

                • awardwinningfjords.com

                • github.com/secondstory

                • github.com/tdreyno

                • http://github.com/tdreyno/jquery-demo



Sunday, October 17, 2010

More Related Content

PDF
OOCSS for JavaScript Pirates jQcon Boston
PDF
jQuery: Nuts, Bolts and Bling
PDF
Write Less Do More
PDF
Functionality Focused Code Organization
PPTX
Unobtrusive javascript with jQuery
PPTX
jQuery Fundamentals
PDF
jQuery Features to Avoid
OOCSS for JavaScript Pirates jQcon Boston
jQuery: Nuts, Bolts and Bling
Write Less Do More
Functionality Focused Code Organization
Unobtrusive javascript with jQuery
jQuery Fundamentals
jQuery Features to Avoid

What's hot (20)

KEY
jQuery('#knowledge').appendTo('#you');
PDF
jQuery Loves Developers - Oredev 2009
PDF
jQuery Essentials
ZIP
OOCSS for Javascript pirates at jQueryPgh meetup
PPTX
jQuery
PDF
jQuery Introduction
PPTX
Getting the Most Out of jQuery Widgets
PPTX
PDF
Learning jQuery in 30 minutes
PDF
jQuery for beginners
PPTX
A Rich Web experience with jQuery, Ajax and .NET
PPT
JQuery introduction
PDF
jQuery Basic API
PDF
Stack Overflow Austin - jQuery for Developers
PDF
Prototype & jQuery
PDF
Dojo Confessions
KEY
The jQuery Library
PDF
jQuery in the [Aol.] Enterprise
PDF
Learning jQuery made exciting in an interactive session by one of our team me...
PPT
jQuery('#knowledge').appendTo('#you');
jQuery Loves Developers - Oredev 2009
jQuery Essentials
OOCSS for Javascript pirates at jQueryPgh meetup
jQuery
jQuery Introduction
Getting the Most Out of jQuery Widgets
Learning jQuery in 30 minutes
jQuery for beginners
A Rich Web experience with jQuery, Ajax and .NET
JQuery introduction
jQuery Basic API
Stack Overflow Austin - jQuery for Developers
Prototype & jQuery
Dojo Confessions
The jQuery Library
jQuery in the [Aol.] Enterprise
Learning jQuery made exciting in an interactive session by one of our team me...
Ad

Viewers also liked (13)

PDF
Is these a bug
PPTX
You and Your Stylesheet
PPT
Html5 accessibility
PDF
Clase auxiliar transito_y_calculo_de_ejes_equivalentes
PDF
Lab pavimentos
KEY
Sass Introduction
PDF
Manual de diseño de pavimentos
PDF
Practica dirigida de mezclas asfálticas
PPT
Twitter For Writers
PDF
How To Build iOS Apps Without interface Builder
PDF
App in a Browser
KEY
HTML5 Web Messaging
DOCX
Diseodeescaleradeconcretoarmado
Is these a bug
You and Your Stylesheet
Html5 accessibility
Clase auxiliar transito_y_calculo_de_ejes_equivalentes
Lab pavimentos
Sass Introduction
Manual de diseño de pavimentos
Practica dirigida de mezclas asfálticas
Twitter For Writers
How To Build iOS Apps Without interface Builder
App in a Browser
HTML5 Web Messaging
Diseodeescaleradeconcretoarmado
Ad

Similar to Organizing Code with JavascriptMVC (20)

KEY
Week 4 - jQuery + Ajax
PPT
Jquery Best Practices
PDF
Cleaner, Leaner, Meaner: Refactoring your jQuery
KEY
[Coscup 2012] JavascriptMVC
PDF
Intro to HTML5 Web Storage
PDF
Web Crawling with NodeJS
PDF
Writing Maintainable JavaScript
PPTX
Jquery optimization-tips
PDF
jQuery - 10 Time-Savers You (Maybe) Don't Know
PDF
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
PDF
bcgr3-jquery
PDF
bcgr3-jquery
PPTX
J query1
KEY
Introduction to jQuery - Barcamp London 9
PPT
JavaScript JQUERY AJAX
PPTX
Jquery introduction
KEY
jQuery: Tips, tricks and hints for better development and Performance
PPTX
Taming that client side mess with Backbone.js
PPTX
2a-JQuery AJAX.pptx
PDF
Trimming The Cruft
Week 4 - jQuery + Ajax
Jquery Best Practices
Cleaner, Leaner, Meaner: Refactoring your jQuery
[Coscup 2012] JavascriptMVC
Intro to HTML5 Web Storage
Web Crawling with NodeJS
Writing Maintainable JavaScript
Jquery optimization-tips
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
bcgr3-jquery
bcgr3-jquery
J query1
Introduction to jQuery - Barcamp London 9
JavaScript JQUERY AJAX
Jquery introduction
jQuery: Tips, tricks and hints for better development and Performance
Taming that client side mess with Backbone.js
2a-JQuery AJAX.pptx
Trimming The Cruft

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Modernizing your data center with Dell and AMD
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Unlocking AI with Model Context Protocol (MCP)
PPT
Teaching material agriculture food technology
PPTX
Cloud computing and distributed systems.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
NewMind AI Monthly Chronicles - July 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Modernizing your data center with Dell and AMD
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Understanding_Digital_Forensics_Presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Unlocking AI with Model Context Protocol (MCP)
Teaching material agriculture food technology
Cloud computing and distributed systems.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx

Organizing Code with JavascriptMVC

  • 1. Organizing Code with JavaScriptMVC git clone git://github.com/tdreyno/jquery-demo.git Thomas Reynolds Sunday, October 17, 2010
  • 2. Who am I? Sunday, October 17, 2010
  • 3. Who am I? • JavascriptMVC Core Committer • jQuery Plugin Developer • @tdreyno • github.com/tdreyno • awardwinningfjords.com Sunday, October 17, 2010
  • 6. Getting to Know You Sunday, October 17, 2010
  • 7. Getting to Know You • Traditional Website Developers • Web Application Developers • Mobile Web Developers Sunday, October 17, 2010
  • 10. One BIG File Sunday, October 17, 2010
  • 11. $(function(){ // make external links open in new window $('a[rel="external"]').attr('target','_blank'); if ($("body").attr('id') == 'homepage'){ bindHomepage(); } else if ($("body").hasClass('lessons_index') || $("body").hasClass('lessons_list')) { bindLessonsList(); } else if ($("body").hasClass('documents_detail')) { if (weird === 0){ showDetail(); } weird++; } else if ($("body").hasClass('lessons_detail')) { if (weird === 0){ showDetail(); } weird++; // look for requests to launch the activity editor var edit_matches = window.location.search.match(/[?&]edit_activity=(d+)&type=([^=?&]+)/) if (edit_matches) { var edit_activity_id = edit_matches[1]; var edit_activity_type = edit_matches[2]; launchCreatorModal({ id: edit_activity_id, type: edit_activity_type, mode: 'edit' }); } } else if ($("body").attr('id')=='my_classroom'){ bindMyClassroom(); } else if ($("body").hasClass('templates_index')){ bindTemplates(); } bindCreatorLinks(); // This can cause an error in Chrome, but the problem is already patched (03/2010) // and awaiting a release. window.onbeforeunload = handleOnBeforeUnload; window.onunload = handleOnUnload; if (window.location.search.match(/[?&]from_activity=1/)) { $('#back_to_activity a').pulse(); } bindSearchToggle(); // Load the "Star This" code // Should be inlined using something like Sprockets in the future $.getScript("/js/stars-and-add-to-buttons.js"); // Handle resources page toggles $("p.more a.morelink, ul.more a.morelink, a.arrow").click(function(e) { e.preventDefault(); var parentElem = $(this).parents(".openRow, .closedRow"); parentElem[0].className = parentElem.is(".openRow") ? "closedRow" : "openRow"; }); if (document.location.hash.length) { $("a[name=" + document.location.hash.replace(/#/, "") + "]").parents(".closedRow").find(".arrow").click(); Sunday, October 17, 2010 }
  • 12. One BIG File • 800+ Lines • Difficult to read • No Documentation • No Test Cases • Not DRY Sunday, October 17, 2010
  • 13. The Solution? Break into Maintainable Chunks of Code Sunday, October 17, 2010
  • 14. Problems with Multiple Files • Does everything go into one folder? • What if something is needed in two different places? • Communication • Loading speed • Loading order Sunday, October 17, 2010
  • 15. JavascriptMVC Provides Dependency Management Sunday, October 17, 2010
  • 16. StealJS • Asynchronously Loading • Provides a structure for organizing files • Cleans, Combines & Compresses • Bundles Presentational CSS • Supports CoffeeScript & Less.js Sunday, October 17, 2010
  • 17. StealJS Example steal.plugins("jquery/controller", "jquery/event/drag", "jquery/dom/within") .then(function($) { // Configure JavascriptMVC plugins }) .resources("jquerytools.tabs", "jquery.ui.position", "jquerytools.scrollable") .then(function($) { // Configure jQuery plugins }) .controllers("tile", "large_zoom", "medium_zoom", "small_zoom", "control", "image") .then(function($) { // All dependencies are satisfied }) Sunday, October 17, 2010
  • 18. Plugin • Basic unit of organization • Simply, a folder. • Initialized by a .js file named the same as the app. • For example: my_plugin/my_plugin.js • Can include other plugins Sunday, October 17, 2010
  • 19. Resources • A folder named “resources/” in your plugin • Contains raw Javascript files, often a jQuery plugin • jQuery UI • jQuery Tools • Modernizr • Et cetera Sunday, October 17, 2010
  • 20. Controller • Could be called several things: • View Controller • Widget • $.fn on Steroids Sunday, October 17, 2010
  • 21. Controller • Controls a DOM element • Uses templates to create & manipulate the DOM • Manages state • Uses PubSub (OpenAjax) to communicate • Responds to events Sunday, October 17, 2010
  • 25. Example Site • Homepage • A slideshow • Tabs • Custom Autocomplete Search • Contact Page • Form with Validator • “Success” lightbox • Custom Autocomplete Search Sunday, October 17, 2010
  • 26. Getting Started • Download zip file from GitHub • http://github.com/jupiterjs/framework/ downloads • Unzip • Install gem • gem install javascriptmvc --pre • jmvc-init jqconf-demo Sunday, October 17, 2010
  • 27. Fresh JavascriptMVC Application Sunday, October 17, 2010
  • 29. Homepage HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <title>homepage</title> </head> <body> <h1>Thanks for stealing StealJS!</h1> <p>Don't worry, it's open source. It's only stealing if you don't do something awesome with it.</p> <div id='content'></div> <script type='text/javascript' src='../steal/steal.js?homepage'></script> </body> </html> Sunday, October 17, 2010
  • 30. Homepage Javascript steal( 'resources/example' ) // Loads 'resources/example.js' .css( 'homepage' ) // Loads 'homepage.css' .plugins( 'steal/less', 'steal/coffee' ) // Loads 'steal/less/less.js' and // 'steal/coffee/coffee.js' .then(function(){ // Adds a function to be called back // once all prior files have been // loaded and run steal.coffee('resources/example') // Loads 'resources/example.coffee' .less('resources/example'); // Loads 'resources/example.less' }); Sunday, October 17, 2010
  • 31. Resources (jQuery Plugins) Sunday, October 17, 2010
  • 32. homepage/homepage.js steal.plugins("jquery") .resources("slider", "tabs") .then(function($) { $(document).ready(function() { $("#slider").slider(); $("#tabs").tabs(); }); }); Sunday, October 17, 2010
  • 34. Contact Page “App” Sunday, October 17, 2010
  • 35. Contact Page steal.plugins("jquery") .resources("validator", "lightbox") .then(function() { $("form").validator(); $("form").submit(function() { $.lightbox(); }); }); Sunday, October 17, 2010
  • 36. Autocomplete Search (Plugin) Sunday, October 17, 2010
  • 37. TR.SearchControl Plugin Sunday, October 17, 2010
  • 39. FuncUnit • Test-Driven Development: • Results well thought-out code • Inspires trust in your users • Documents logic • Provides an automated means of making sure everything still works Sunday, October 17, 2010
  • 40. TR.SearchControl FuncUnit module("TR.SearchControl", { setup: function() { $("form#search").tr_search_control(); } }) test("Autocomplete", function(){ // Type into text box // Extra results should appear }) test("Submit", function(){ // Clicking submit or hitting enter // Should submit the form }) Sunday, October 17, 2010
  • 41. TR.SearchControl FuncUnit Sunday, October 17, 2010
  • 42. Search Controller steal.plugins("jquery/controller") .resources("autocomplete") .then(function($) { $.Controller.extend("TR.SearchControl", { }, { init: function() { this.element.autocomplete(); }, "input:text change": function() { // Autocomplete lookup }, "submit": function() { // Ajax form submit } }); $(document).ready(function() { $("form#search").tr_search_control(); }); }); Sunday, October 17, 2010
  • 43. TR.SearchControl FuncUnit Sunday, October 17, 2010
  • 44. homepage/homepage.js steal.plugins("jquery") .resources("slider", "tabs") .plugins("tr/search_control") .then(function($) { $(document).ready(function() { $("#slider").slider(); $("#tabs").tabs(); }); }); Sunday, October 17, 2010
  • 46. All Done Coding, Time to Optimize Sunday, October 17, 2010
  • 47. Building & Compression • Packages • 0.js - Shared between all apps (jQuery, JavascriptMVC componetns, etc) • homepage/production.js Unique to Homepage • contacts/contact.js Unique to Contact • Google Closure Compiler • Dojo Shrinksafe also available Sunday, October 17, 2010
  • 48. Building & Compression ./steal/js steal/buildjs homepage contact Sunday, October 17, 2010
  • 49. Building & Compression Sunday, October 17, 2010
  • 50. Production HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <title>homepage</title> </head> <body> <h1>Thanks for stealing StealJS!</h1> <p>Don't worry, it's open source. It's only stealing if you don't do something awesome with it.</p> <div id='content'></div> <script type='text/javascript' src='../steal/steal.js?homepage,production'> </script> </body> </html> Sunday, October 17, 2010
  • 52. Summary • Organize code into apps & plugins • Always Test Your Code • Use consistent naming and organization • Create sharable modules (Github) • Optimize! Sunday, October 17, 2010
  • 53. JavascriptMVC Provides Models (JSON API wrappers) Views (EJS, Micro, Jaml) Controllers (Event system, data binding) Functional testing (FuncUnit, qunit, selenium) Dependency management (Optimization) Sunday, October 17, 2010
  • 54. My Plugins JavascriptMVC convenience gem • gem install javascriptmvc --pre • jmvc-init newproject • jmvc-gen newapp Sunday, October 17, 2010
  • 55. My Plugins • steal.plugins("ss/state_machine") http://github.com/secondstory/secondstoryjs- statemachine • steal.plugins("ss/router") http://github.com/secondstory/secondstoryjs-router • steal.plugins("tr/views/mustache") http://github.com/tdreyno/mustache-javascriptmvc • steal.plugins("tr/controller/pure") http://github.com/tdreyno/javascriptmvc-pure Sunday, October 17, 2010
  • 56. Thank you • Questions? • http://v3.javascriptmvc.com/index.html • @tdreyno • awardwinningfjords.com • github.com/secondstory • github.com/tdreyno • http://github.com/tdreyno/jquery-demo Sunday, October 17, 2010