SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
Deep Dive Into Backbone.js Internals +
Underscore.js.
Mauvis Ledford
CTO, Pathbrite
Backbone.js: Quick Facts

•  0.1.0 released Oct 13, 2010, latest stable 0.9.2 – 7 months ago
•  Used in dozens of popular web and mobile web sites out there:




•  Soundcloud, Stripe, Grooveshark, Do, BitTorrent, Nike+, …


2
Backbone.js: Source Code Analysis

Lines of code
•  1432 lines w/ comments.
•  856 lines without.


Size
•  52kb raw w/ comments
•  30k without
•  5.6kb packed and gzipped


Underscore
•  4kb packed and gzipped

3
Backbone.js: 856 line breakdown

•  Model (234)              inheritance,
                                2%
•  Collection (218)                             misc,
                         .sync, 3%
•  View (75)                                     8%
                                                             Model,
•  Router (46)                                                29%
•  History (116)                   History,
                                    15%
•  Event (65)
•  Sync (29)
                      Router, 6%
•  Inherit (17)                         View,           Collection,
•  Misc (55)                            10%               27%




4
How to build a good framework?
Starts with these 3.




5                      Taken from the PunyMCE framework.
Same three functions in other libraries

jQuery
•  jQuery.each
•  jQuery.extend
•  jQuery.isFunction, jQuery.isArray, jQuery.isEmptyObject, jQuery.isNumeric


Underscore
•  _.each
•  _.extend
•  _.isFunction, _.isArray, _.isEmpty, _.has, _.isEqual




6
_.extend({},{},…)
Backbone source uses this heavily.




7
Diving into the code: top down
Environment setup.
                                            Anonymous function
                                               to encapsulate
                                             framework setup.
                                                      	
  




       Calling anonymous
    function in the context of
             global object.
    Node:	
  global	
  DOM:	
  window	
  
8
Diving into the code: top down
Two checks: “exports” and “require”.
                                                           Preparing for jQuery-
…	
                                                          like “noConflict”
                                                                     	
  


                                                              Checks for existence of
                                                            global “exports” variable. If
                                                              exists, assume we are
                                                                    serverside. 	
  




…	
  
                                   Similarily, if underscore isn’t loaded and global
                                      “require” exists include underscore lib.
                                                            	
  
9       Let your knowledge shine
Diving into the code: top down                Supports jQuery,
NoConflict and optional DOM library.          Zepto, or Ender	
  

…	
  



                                                          Allows you to
                                                        restore Backbone
                                                          namespace to
                                                       previous Backbone.	
  




…	
  
        Used with Backbone.sync method.	
  


10
Diving into the code: top down
Event class, so important!
…	
  
                                                   All of Backbone’s
                                                  classes inherit this
                                                    class and now
                                                       have these
                                                        methods.	
  




…	
  
                    Backwards compatibility	
  
11
Diving into the code: top down
Mixing Event class in to custom class.

…	
  




…	
  




12
Diving into the code: top down
Model class
…	
  




                             Every new model gets a unique id.	
  


                                  Attributes are set silently on
                                         model creation.
                                                	
  
                                 Last step, call the models init.
                                                 	
  
…	
  
13
Diving into the code: top down
Model class

…	
  


                                Notice that `Events` class
                             extends `Model.prototype` which
                                  is then extended by an
                                    anonymous object.	
  




…	
  
14
Diving into the code: top down
Collection class
                             If `comparator` function is passed
                              new models are sorted in order.	
  
…	
  




…	
  




15
Diving into the code: top down
Collection class
…	
  

                                 Notice that `Events` class is
                                       mixed in again.	
  




                                          Additional underscore
                                           methods are added
                                                directly to
                                          `Collection.prototype`.	
  
…	
  
16
Diving into the code: top down
Demonstrating models and collections on https://pathbrite.com/portfolios
.
    // Count portfolios
    rrripple.data.portfolios.length;

    // Get collection JSON list.
    rrripple.data.portfolios.toJSON();

    // Get model JSON list.
    rrripple.data.portfolios.first().toJSON();

    // Add new portfolio item.
    rrripple.data.portfolios.add({
        title : 'Watermelon'
    });




17
Diving into the code: top down
Router class.

…	
  




…	
  

  Show	
  sample	
  routes.	
  

18
Diving into the code: top down
History class

…	
  




…	
  
 Backbone.history.navigate('/portfolios', {
     trigger: true
 });

19
Diving into the code: top down
View class

…	
  
                                                                 `ensureElement` sets
                                                              `this.el` to a detached div if
                                                              no el or tagName specified.	
  



                                                          Arguments passed with these
                                                          keys are applied directly to the
                                                              instantiated view during
                                                           `this._configure`, the rest can
                                                             be found in `view.options`.	
  
…	
  
     Show:	
  rp.classes.views.popup	
  in	
  editor.	
  
     var	
  view	
  =	
  new	
  rrripple.classes.views.par>als.Se?ngsPopup();	
  
20
Diving into the code: top down
Inherits functionality
…	
  




                 User.prototype.save = function(attrs) {
                     // modify attrs code
                     User.__super__.save.apply(this, arguments);
                 };


                                    Shh! Saved referenced to parents
                                       prototype. Let’s you access
                                     parents method you could have
                                         overwritten on the child.	
  
…	
  
21
Diving into the code: top down
Giving all classes extend ability




…	
  




…	
  
                 Notice `_.extend` != `Collection.extend`!!!	
  




22
Live Demos
Frontend and backend Backbone examples

                                                    Pathbrite:	
  Backbone	
  as	
  
                                                    a	
  frontend	
  plaGorm.	
  
                                                    	
  
                                                    pathbrite.com	
  




     Kbot:	
  Turntable	
  robot	
  built	
  	
  
        on	
  Node	
  and	
  Backbone.	
  
                                          	
  
 github.com/krunkosaurus/kbot	
  
                                          	
  
23                                        	
  
Thanks!

Feedback?
mauvis@pathbrite.com
@krunkosaurus




24
Last year: Don’t Break the Chain
Sample responsive Backbone app.




                        hKp://dontbreak.me/	
  
                                 	
  
25

Weitere ähnliche Inhalte

Was ist angesagt?

Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Anton Arhipov
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesCharles Nutter
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
Rapid java application development @ JUG.ru 25.02.2012
Rapid java application development @ JUG.ru 25.02.2012Rapid java application development @ JUG.ru 25.02.2012
Rapid java application development @ JUG.ru 25.02.2012Anton Arhipov
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMashleypuls
 

Was ist angesagt? (11)

Java 10, Java 11 and beyond
Java 10, Java 11 and beyondJava 10, Java 11 and beyond
Java 10, Java 11 and beyond
 
ClassJS
ClassJSClassJS
ClassJS
 
Class
ClassClass
Class
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
 
From dot net_to_rails
From dot net_to_railsFrom dot net_to_rails
From dot net_to_rails
 
Cappuccino
CappuccinoCappuccino
Cappuccino
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Rapid java application development @ JUG.ru 25.02.2012
Rapid java application development @ JUG.ru 25.02.2012Rapid java application development @ JUG.ru 25.02.2012
Rapid java application development @ JUG.ru 25.02.2012
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASM
 

Ähnlich wie Deep Dive Into Backbone.js and Underscore.js Internals

The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014Matthias Noback
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvarsSam Marley-Jarrett
 
The Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony BarcelonaThe Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony BarcelonaMatthias Noback
 
The Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup BelgiumThe Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup BelgiumMatthias Noback
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.jsBert Wijnants
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends旻琦 潘
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design PatternsAddy Osmani
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworksMD Sayem Ahmed
 
Professional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsMike Wilcox
 
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012skinandbones
 
What's New In Python 2.6
What's New In Python 2.6What's New In Python 2.6
What's New In Python 2.6Richard Jones
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller ColumnsJonathan Fine
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of featuresvidyamittal
 

Ähnlich wie Deep Dive Into Backbone.js and Underscore.js Internals (20)

The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvars
 
The Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony BarcelonaThe Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony Barcelona
 
The Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup BelgiumThe Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup Belgium
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor BuzatovićJavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Js tacktalk team dev js testing performance
Js tacktalk team dev js testing performanceJs tacktalk team dev js testing performance
Js tacktalk team dev js testing performance
 
Migrating to Java 9 Modules
Migrating to Java 9 ModulesMigrating to Java 9 Modules
Migrating to Java 9 Modules
 
Dependency injectionpreso
Dependency injectionpresoDependency injectionpreso
Dependency injectionpreso
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design Patterns
 
Play framework
Play frameworkPlay framework
Play framework
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
 
Professional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatterns
 
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
 
What's New In Python 2.6
What's New In Python 2.6What's New In Python 2.6
What's New In Python 2.6
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of features
 

Deep Dive Into Backbone.js and Underscore.js Internals

  • 1. Deep Dive Into Backbone.js Internals + Underscore.js. Mauvis Ledford CTO, Pathbrite
  • 2. Backbone.js: Quick Facts •  0.1.0 released Oct 13, 2010, latest stable 0.9.2 – 7 months ago •  Used in dozens of popular web and mobile web sites out there: •  Soundcloud, Stripe, Grooveshark, Do, BitTorrent, Nike+, … 2
  • 3. Backbone.js: Source Code Analysis Lines of code •  1432 lines w/ comments. •  856 lines without. Size •  52kb raw w/ comments •  30k without •  5.6kb packed and gzipped Underscore •  4kb packed and gzipped 3
  • 4. Backbone.js: 856 line breakdown •  Model (234) inheritance, 2% •  Collection (218) misc, .sync, 3% •  View (75) 8% Model, •  Router (46) 29% •  History (116) History, 15% •  Event (65) •  Sync (29) Router, 6% •  Inherit (17) View, Collection, •  Misc (55) 10% 27% 4
  • 5. How to build a good framework? Starts with these 3. 5 Taken from the PunyMCE framework.
  • 6. Same three functions in other libraries jQuery •  jQuery.each •  jQuery.extend •  jQuery.isFunction, jQuery.isArray, jQuery.isEmptyObject, jQuery.isNumeric Underscore •  _.each •  _.extend •  _.isFunction, _.isArray, _.isEmpty, _.has, _.isEqual 6
  • 8. Diving into the code: top down Environment setup. Anonymous function to encapsulate framework setup.   Calling anonymous function in the context of global object. Node:  global  DOM:  window   8
  • 9. Diving into the code: top down Two checks: “exports” and “require”. Preparing for jQuery- …   like “noConflict”   Checks for existence of global “exports” variable. If exists, assume we are serverside.   …   Similarily, if underscore isn’t loaded and global “require” exists include underscore lib.   9 Let your knowledge shine
  • 10. Diving into the code: top down Supports jQuery, NoConflict and optional DOM library. Zepto, or Ender   …   Allows you to restore Backbone namespace to previous Backbone.   …   Used with Backbone.sync method.   10
  • 11. Diving into the code: top down Event class, so important! …   All of Backbone’s classes inherit this class and now have these methods.   …   Backwards compatibility   11
  • 12. Diving into the code: top down Mixing Event class in to custom class. …   …   12
  • 13. Diving into the code: top down Model class …   Every new model gets a unique id.   Attributes are set silently on model creation.   Last step, call the models init.   …   13
  • 14. Diving into the code: top down Model class …   Notice that `Events` class extends `Model.prototype` which is then extended by an anonymous object.   …   14
  • 15. Diving into the code: top down Collection class If `comparator` function is passed new models are sorted in order.   …   …   15
  • 16. Diving into the code: top down Collection class …   Notice that `Events` class is mixed in again.   Additional underscore methods are added directly to `Collection.prototype`.   …   16
  • 17. Diving into the code: top down Demonstrating models and collections on https://pathbrite.com/portfolios . // Count portfolios rrripple.data.portfolios.length; // Get collection JSON list. rrripple.data.portfolios.toJSON(); // Get model JSON list. rrripple.data.portfolios.first().toJSON(); // Add new portfolio item. rrripple.data.portfolios.add({ title : 'Watermelon' }); 17
  • 18. Diving into the code: top down Router class. …   …   Show  sample  routes.   18
  • 19. Diving into the code: top down History class …   …   Backbone.history.navigate('/portfolios', { trigger: true }); 19
  • 20. Diving into the code: top down View class …   `ensureElement` sets `this.el` to a detached div if no el or tagName specified.   Arguments passed with these keys are applied directly to the instantiated view during `this._configure`, the rest can be found in `view.options`.   …   Show:  rp.classes.views.popup  in  editor.   var  view  =  new  rrripple.classes.views.par>als.Se?ngsPopup();   20
  • 21. Diving into the code: top down Inherits functionality …   User.prototype.save = function(attrs) { // modify attrs code User.__super__.save.apply(this, arguments); }; Shh! Saved referenced to parents prototype. Let’s you access parents method you could have overwritten on the child.   …   21
  • 22. Diving into the code: top down Giving all classes extend ability …   …   Notice `_.extend` != `Collection.extend`!!!   22
  • 23. Live Demos Frontend and backend Backbone examples Pathbrite:  Backbone  as   a  frontend  plaGorm.     pathbrite.com   Kbot:  Turntable  robot  built     on  Node  and  Backbone.     github.com/krunkosaurus/kbot     23  
  • 25. Last year: Don’t Break the Chain Sample responsive Backbone app. hKp://dontbreak.me/     25