SlideShare ist ein Scribd-Unternehmen logo
1 von 56
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');mikehostetler
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009Remy Sharp
 
OOCSS for Javascript pirates at jQueryPgh meetup
OOCSS for Javascript pirates at jQueryPgh meetupOOCSS for Javascript pirates at jQueryPgh meetup
OOCSS for Javascript pirates at jQueryPgh meetupBrian Cavalier
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgetsvelveeta_512
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutesSimon Willison
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETJames Johnson
 
Stack Overflow Austin - jQuery for Developers
Stack Overflow Austin - jQuery for DevelopersStack Overflow Austin - jQuery for Developers
Stack Overflow Austin - jQuery for DevelopersJonathan Sharp
 
Prototype & jQuery
Prototype & jQueryPrototype & jQuery
Prototype & jQueryRemy Sharp
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterpriseDave Artz
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Thinqloud
 

Was ist angesagt? (20)

jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
OOCSS for Javascript pirates at jQueryPgh meetup
OOCSS for Javascript pirates at jQueryPgh meetupOOCSS for Javascript pirates at jQueryPgh meetup
OOCSS for Javascript pirates at jQueryPgh meetup
 
jQuery
jQueryjQuery
jQuery
 
jQuery Introduction
jQuery IntroductionjQuery Introduction
jQuery Introduction
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgets
 
jQuery
jQueryjQuery
jQuery
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NET
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
jQuery Basic API
jQuery Basic APIjQuery Basic API
jQuery Basic API
 
Stack Overflow Austin - jQuery for Developers
Stack Overflow Austin - jQuery for DevelopersStack Overflow Austin - jQuery for Developers
Stack Overflow Austin - jQuery for Developers
 
Prototype & jQuery
Prototype & jQueryPrototype & jQuery
Prototype & jQuery
 
Dojo Confessions
Dojo ConfessionsDojo Confessions
Dojo Confessions
 
The jQuery Library
The  jQuery LibraryThe  jQuery Library
The jQuery Library
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...
 
jQuery
jQueryjQuery
jQuery
 

Andere mochten auch

Clase auxiliar transito_y_calculo_de_ejes_equivalentes
Clase auxiliar transito_y_calculo_de_ejes_equivalentesClase auxiliar transito_y_calculo_de_ejes_equivalentes
Clase auxiliar transito_y_calculo_de_ejes_equivalentesoscar torres
 
Manual de diseño de pavimentos
Manual de diseño de pavimentosManual de diseño de pavimentos
Manual de diseño de pavimentososcar torres
 
Practica dirigida de mezclas asfálticas
Practica dirigida de mezclas asfálticasPractica dirigida de mezclas asfálticas
Practica dirigida de mezclas asfálticasoscar torres
 
How To Build iOS Apps Without interface Builder
How To Build iOS Apps Without interface BuilderHow To Build iOS Apps Without interface Builder
How To Build iOS Apps Without interface Builderdasdom
 
HTML5 Web Messaging
HTML5 Web MessagingHTML5 Web Messaging
HTML5 Web MessagingMike Taylor
 
Diseodeescaleradeconcretoarmado
DiseodeescaleradeconcretoarmadoDiseodeescaleradeconcretoarmado
Diseodeescaleradeconcretoarmadooscar torres
 

Andere mochten auch (13)

Is these a bug
Is these a bugIs these a bug
Is these a bug
 
You and Your Stylesheet
You and Your StylesheetYou and Your Stylesheet
You and Your Stylesheet
 
Html5 accessibility
Html5 accessibilityHtml5 accessibility
Html5 accessibility
 
Clase auxiliar transito_y_calculo_de_ejes_equivalentes
Clase auxiliar transito_y_calculo_de_ejes_equivalentesClase auxiliar transito_y_calculo_de_ejes_equivalentes
Clase auxiliar transito_y_calculo_de_ejes_equivalentes
 
Lab pavimentos
Lab pavimentosLab pavimentos
Lab pavimentos
 
Sass Introduction
Sass IntroductionSass Introduction
Sass Introduction
 
Manual de diseño de pavimentos
Manual de diseño de pavimentosManual de diseño de pavimentos
Manual de diseño de pavimentos
 
Practica dirigida de mezclas asfálticas
Practica dirigida de mezclas asfálticasPractica dirigida de mezclas asfálticas
Practica dirigida de mezclas asfálticas
 
Twitter For Writers
Twitter For WritersTwitter For Writers
Twitter For Writers
 
How To Build iOS Apps Without interface Builder
How To Build iOS Apps Without interface BuilderHow To Build iOS Apps Without interface Builder
How To Build iOS Apps Without interface Builder
 
App in a Browser
App in a BrowserApp in a Browser
App in a Browser
 
HTML5 Web Messaging
HTML5 Web MessagingHTML5 Web Messaging
HTML5 Web Messaging
 
Diseodeescaleradeconcretoarmado
DiseodeescaleradeconcretoarmadoDiseodeescaleradeconcretoarmado
Diseodeescaleradeconcretoarmado
 

Ähnlich wie Organizing Code with JavascriptMVC

Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajaxbaygross
 
Jquery Best Practices
Jquery Best PracticesJquery Best Practices
Jquery Best Practicesbrinsknaps
 
Cleaner, Leaner, Meaner: Refactoring your jQuery
Cleaner, Leaner, Meaner: Refactoring your jQueryCleaner, Leaner, Meaner: Refactoring your jQuery
Cleaner, Leaner, Meaner: Refactoring your jQueryRebecca Murphey
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 
Intro to HTML5 Web Storage
Intro to HTML5 Web StorageIntro to HTML5 Web Storage
Intro to HTML5 Web Storagedylanks
 
Web Crawling with NodeJS
Web Crawling with NodeJSWeb Crawling with NodeJS
Web Crawling with NodeJSSylvain Zimmer
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScriptAndrew Dupont
 
Jquery optimization-tips
Jquery optimization-tipsJquery optimization-tips
Jquery optimization-tipsanubavam-techkt
 
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Knowgirish82
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)Doris Chen
 
Introduction to jQuery - Barcamp London 9
Introduction to jQuery - Barcamp London 9Introduction to jQuery - Barcamp London 9
Introduction to jQuery - Barcamp London 9Jack Franklin
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformanceJonas De Smet
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsJarod Ferguson
 
2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptxLe Hung
 

Ähnlich wie Organizing Code with JavascriptMVC (20)

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

Kürzlich hochgeladen

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

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