SlideShare una empresa de Scribd logo
1 de 80
Descargar para leer sin conexión
JavaScript
HELLO
                     my name is




              @AARONIUS   · AARONHARDY.COM
Software Engineer, Adobe Digital Marketing Suite
   We enable management, measurement, execution, and
     optimization of digital advertising and marketing.
No hidden agenda
Single-Page Apps
TabBar
  CurrencyFormatter

   EventDispatcher        List     ArrayCollection
                 DataGroup
DragManager                           ToggleButtonBar
                  Panel
                                      NumericStepper
    Slider
                ViewStack
                                  Accordion
Bindable
                  DataGrid                Effects
AccountInfo
                                   (Group)
                   Header
                  (Group)
                                    Menu
                              (ToggleButtonBar)


     Main
                                                    Biography
(Application)
                                                     (Group)
                                   Artist
                                  (Group)
                                                  Similar Artists
                                                       (List)
                   Music        Discography
                (ViewStack)     (DataGroup)


                                Discussion
                                (DataGroup)
?                  Links          ?
          Basic Form Elements
Div
          Image      ?               Bulleted Lists

                                         ?
 ?                Span           ?
<html>
    <head>
        <script>
            buckets of vomit
            buckets of vomit
            buckets of vomit
            buckets of vomit
            buckets of vomit
        </script>
    </head>
    <body>
        buckets of vomit
        buckets of vomit
        buckets of vomit
        buckets of vomit
        buckets of vomit
    </body>
</html>
•   IDEs
•   MVC
•   Widgets/Components
•   Dependency management
•   DOM manipulation
•   Templating
•   Testing
•   Utilities
•   JavaScript + HTML + CSS
Quality ide
Libraries
Rolling your own
jquery
    underscore.js
     backbone.js
      requirejs
All MIT license (+ other options) and on GitHub
Learn Javascript
Never build large apps
 The secret to building large apps is NEVER build
 large apps. Break up your applications into small
 pieces. Then, assemble those testable, bite-sized
 pieces into your big application.
 – Justin Meyer
Admit what you
      don’t know
The key is to acknowledge from the start that you
have no idea how this will grow. When you accept
that you don’t know everything, you begin to
design the system defensively.
– Nicholas Zakas
Add "use strict"; before code or at beginning of
function to help decrapify your code.

• Eliminates pitfalls by raising errors
• Improves JS engine optimization
• Prohibits syntax reserved for future JS versions

* Won’t be used in examples to save slide space
JSLint or JSHint.

•   Run in IDE.
•   Run on command line.
•   Run in build scripts.
•   Copy-paste into web interface.
•   Use flags for configuration.
General community style:
• Names of directories and files shall be lowercase and
  hyphens shall be used to separate words.
• Variable names shall be camelCase.
• Classes (functions) to be instantiated shall start with a
  capital letter.
• Members intended to be private shall be prefixed with
  an underscore.
Loose typing == more testing!

•   JsTestDriver
•   QUnit
•   Jasmine
•   Vows
•   …and bucket-loads more
jquery
• Not an application framework!
• Abstracts the DOM
    • Style
    • Position
    • Behavior
    • Animation
    • Creation/Removal
• AJAX requests
• Utilities for objects, arrays, strings, etc.
• “Framework” for UI widgets and utilities
Usage by Top Sites




http://trends.builtwith.com/javascript/jQuery
• Performance-tuned servers
• Allows browser to have more concurrent connections
• Cross-site caching
Toolbox of array, object, and function manipulation
utilities.
var names = _.pluck(
       [{name: 'moe', age: 40}, {name: 'larry', age: 50}],
       'name');
var person = _.extend({name: 'moe'}, {age: 50});
var uniques = _.union([1, 2, 3], [101, 2, 10], [2, 1]);


And bucket-loads more…
Old-school
var tweetTemplate = '' +
       '<div>' +
               '<div style="float: left">' +
                      '<img src="' + avatar + '"/>' +
               '</div>' +
               '<div style="margin-left: 60px">' +
                      '<p>' + username + '</p>' +
                      '<p>' + text + '</p>' +
               '</div>' +
       '<div>';
Old-school
• HTML buried in logic (hard to re-skin)
• Concatenation/escaping takes time and is error-prone
• IDE might not code-hint or color-code properly
• Boring and tedious
New-school
<script type="text/template" id="tweet-template">
       <div>
               <div style="float: left">
                      <img src="<%= avatar %>"/>
               </div>
               <div style="margin-left: 60px">
                      <p><%= username %></p>
                      <p><%= text %></p>
               </div>
       </div>
</script>
<script>
       var tweet = {avatar: 'aaronius.jpg', alias: '@Aaronius',
               text: 'Honey roasted peanuts rock my sox.'};
       var template = _.template($('#tweet-template').html());
       var html = template(tweet);
       var element = $(html);
</script>
•   Mustache.js
•   Handlebars.js
•   Jade
•   Eco
•   …and bucket-loads more.

Underscore.js templating is one of the best (though not as
full-featured) and is a dependency of Backbone.js. You can
use any templating engine.
backbone
Object A passes a function
to object B and tells
object B to call the
function when X
occurs. When X
occurs, object B calls the
function.
backbone.events
Dad:
var baby = new Baby();

var changeDeuceTrap = function() {
       baby.deuces = 0;
       baby.cakeHoleShut = true;
};

baby.on('turdDropped', changeDeuceTrap);


Baby:
this.trigger('turdDropped');
Mix into any object or class
var baby = new Baby();
baby = _.extend(baby, Backbone.Events);
baby.on(…);
baby.trigger(…);
backbone.Model
Plain Old JavaScript Object
var book = {
       title: 'A Tale of Two Cities',
       author: 'Charles Dickens',
       genre: 'historical',
       publisher: 'Chapman & Hall'
};

How can another object know when one of book’s values
changes? It can’t!
Eventful Backbone.Model
var book = new Backbone.Model({
       title: 'A Tale of Two Cities',
       author: 'Charles Dickens',
       genre: 'historical',
       publisher: 'Chapman & Hall'
});

book.on('change:genre', function(model, genre) {
       alert('genre for ' + model.get('title') +
               ' changed to ' + genre);
});

book.set({genre: 'social criticism'});
Extending Backbone.Model
var Book = Backbone.Model.extend({
       …custom model logic…
});

var book = new Book({
       title: 'A Tale of Two Cities',
       author: 'Charles Dickens',
       publisher: 'Chapman & Hall'
});
Persistence
HTTP Method   URL         Action

GET           /books      Retrieve all books

GET           /books/10   Retrieve book with id == 10

POST          /books      Add a book

PUT           /books/10   Update book with id == 10

DELETE        /books/10   Delete book with id == 10
Persistence (save)
var Book = Backbone.Model.extend({
       urlRoot: '/books'
});

var book = new Book({title: 'The Tragedy of the Commons'});
book.save();
Persistence (fetch)
var Book = Backbone.Model.extend({
       urlRoot: '/books'
});

var book = new Book({id: 2});

var fetchSuccess = function() {
       alert(book.get('title'));
};

book.fetch({success: fetchSuccess});
MODEL
   layersOpen
 characterOpen
magicWandOpen
    colorOpen
     infoOpen
And more…
• Validation
• Extracting native object
• Determine which attributes have changed
• And bucket-loads more…
backbone.collection
Plain Old JavaScript Array:
var books = [book1, book2, book3, book4];

How can another object know when an item is added or
removed from this array? It can’t!
var goodEarth = new Backbone.Model();

var books = new Backbone.Collection();

books.on('add', function(book, books, options) {
       alert('Book ' + book.get('title') + ' added at index ' +
               options.at + '. ' + 'The collection now contains ' +
               books.length + ' models.');
};

books.on('remove', function(book, books, options) {
       alert('Book ' + book.get('title') + ' removed from index ' +
       options.index);
}

books.add(goodEarth);
books.remove(goodEarth);
Event Pass-through
var books = new Backbone.Collection([taleOfTwoCities, goodEarth]);

books.on('change:title', function(book, title) {
       alert('Book title changed from ' +
               book.previous('title') + ' to ' + title);
});

goodEarth.set({title: 'Good Earth, The'});
Persistence (fetch)
var Books = Backbone.Collection.extend({
       model: Book,
       url: '/books'
});

var books = new Books();
books.fetch();
Underscore mix-ins
var titles = books.pluck('title');

each(), reduce(), find(), filter(), reject(), shuffle(), without()
and bucket-loads more!
And more…
• Sorting
• Resetting
• Retrieving model by id or index
• Custom loading and parsing
• …and bucket-loads more!
Backbone view
Stats
        Model


Tweet
Model
        Tweet




        Tweet


                 Tweet
                Collection
        Tweet




        Tweet




        Tweet




        Tweet
var Tweet = Backbone.Model.extend({ …view logic… });
var TweetRow = Backbone.View.extend({ …view logic… });

var tweet = new Tweet({
       avatar: 'aaronius.jpg',
       alias: '@Aaronius',
       text: 'Honey roasted peanuts rock my sox.'
});

var row = new TweetRow({
       model: tweet
});
<script type="text/template" id="tweet-row-template">
       <div style="float: left"><img src="<%= avatar %>"/></div>
       <div style="margin-left: 60px">
               <p><%= username %></p>
               <p><%= text %></p>
       </div>
</script>

var TweetRow = Backbone.View.extend({
       _template: _.template($('#tweet-row-template').html()),

       initialize: function() {
              this.render();
       },

       render: function() {
              this.$el.html(this._template(this.model.toJSON()));
              return this;
       }
});
Backbone router
backbone extensions
requirejs
Must we be restricted to working inside a few monstrous
spaghetti files? NO! Then why are they so prevalent?

• “That’s the way JavaScript has been done in the past.”
• “Loading many JavaScript files requires many HTTP
  requests resulting in longer load times.”
• “Dependency management is hard in JavaScript.”

We can do better!
Old school
// What are the dependencies here!?
// What if a new employee had to re-order for some reason!?

<script   src="script3.js"></script>
<script   src="script1.js"></script>
<script   src="script7.js"></script>
<script   src="script6.js"></script>
<script   src="script4.js"></script>
<script   src="script5.js"></script>
<script   src="script9.js"></script>
<script   src="script8.js"></script>
<script   src="script10.js"></script>
<script   src="script2.js"></script>
script8.js   script4.js   script3.js    script1.js




script2.js                script10.js   script7.js




script9.js   script5.js   script6.js
ServerJS 
      CommonJS 
           Module 
                Async module definition (AMD) 
                      RequireJS
book.js
define({
    title: "My Sister's Keeper",
    publisher: "Atria"
});


bookshelf.js
define([
       'book'
], function(book) {
       return {
               listBook: function() {
                      alert(book.title);
               }
       };
});
index.html
<!DOCTYPE html>
<html>
        <head>
                <title>RequireJS Example</title>
                <script data-main="js/main“
                       src="js/libs/require.js"></script>
        </head>
        <body/>
</html>

main.js
require([
       'bookshelf'
], function(bookshelf) {
       bookshelf.listBook();
});
templates/book.tpl.html
<span class="label">Title:</span>
<span class="value"><%= title %></span><br/>

<span class="label">Author:</span>
<span class="value"><%= author %></span><br/>

<span class="label">Genre:</span>
<span class="value"><%= genre %></span>
book-view.js
define([
       'backbone', 'underscore', 'text!templates/book.tpl.html'
], function(Backbone, _, template) {
       return Backbone.View.extend({
               _template: _.template(template),

              initialize: function() {
                     this.render();
              },

              render: function() {
                     var nativeBook = this.model.toJSON();
                     var html = this._template(nativeBook);
                     this.$el.html(html);
                     return this;
              }
       });
});
bookshelf-view.js
define([
       'backbone',
       'underscore',
       'book-view'
], function(Backbone, _, BookView) {
       return Backbone.View.extend({
               … create child book views as necessary …
               … new BookView() …
       });
});
requirejs optimization
Thank you!

  @Aaronius
aaronhardy.com
encore
(stuff I fit in if time allows)
Persistence with custom url pattern:
var Book = Backbone.Model.extend({
       urlRoot: '/books',
       url: function() {
               return this.urlRoot + '?id=' + this.get('id');
       }
})

var book = new Book({id: 2});
book.fetch();
var App = Backbone.View.extend({
       initialize: function() {
               this.render();
       },

       render: function() {
              var tweet = new Tweet({
                      avatar: 'avatar.jpg',
                      username: '@Aaronius',
                      text: 'Honey roasted peanuts rock my sox.'
              });

              var row = new TweetRow({
                     model: tweet
              });

              this.$el.append(row.$el);
              return this;
       }
});
var app = new App({el: $('body')});
var DocumentView = Backbone.View.extend({
       events: {
               "dblclick"                : "open",
               "click .icon.doc"         : "select",
               "mouseover .title .date" : "showTooltip"
       },

       render: function() { ... },
       open: function() { ... },
       select: function() { ... },
       showTooltip: function() { ... },
});
<script type="text/javascript">
       $(function() {
               var AppRouter = Backbone.Router.extend({
                      routes: {
                              "shirt/id/:id": "showShirt"
                      },

                      showShirt: function(id) {
                             alert('Show shirt with id ' + id);
                      }
              });

              var appRouter = new AppRouter();
              Backbone.history.start();
       });
</script>

<a href="#shirt/id/5">Shirt with id of 5</a><br>
<a href="#shirt/id/10">Shirt with id of 10</a><br>
<a href="#shirt/id/15">Shirt with id of 15</a><br>
<script type="text/javascript">
$(function() {
       var AppRouter = Backbone.Router.extend({
               routes: {
                      ":product/:attribute/:value": "showProduct"
               },

              showProduct: function(product, attribute, value) {
                     alert('Show ' + product + ' where ' +
                             attribute + ' = ' + value + '.');
              }
       });

       var appRouter = new AppRouter();
       Backbone.history.start();
});
</script>

<a href="#shoe/size/12">Size 12 shoes</a><br>
<a href="#shirt/id/5">Shirt with id of 5</a><br>
<a href="#hat/color/black">Black hats</a>
var AppRouter = Backbone.Router.extend({
       routes: {
               ":product/:attribute/:value": "showProduct"
       }
});

var MyView = Backbone.View.extend({
       initialize: function(options) {
               options.router.on('route:showProduct',
                      this._showProduct);
       }

       _showProduct: function(product, attribute, value) {
              alert('Update to show ' + product + ' where '
                      + attribute + ' = ' + value + '.');
       }
});

var appRouter = new AppRouter();
var view = new MyView({router: appRouter});
Backbone.history.start();
Fragment Identifier
• Using URLs in unintended ways
• Dependent on JavaScript
• Controversial
• Used by many popular sites
pushState aka HTML5 History
• The bee’s knees
• Allows changing of core url without changing pages
• Long-term solution
• Browser support lacking (guess which browser)

Más contenido relacionado

La actualidad más candente

A Short Introduction To jQuery
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuerySudar Muthu
 
jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & CompressionjQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & CompressionPaul Irish
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowkatbailey
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsEPAM Systems
 
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
 
Client-side MVC with Backbone.js
Client-side MVC with Backbone.js Client-side MVC with Backbone.js
Client-side MVC with Backbone.js iloveigloo
 
jQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and BlingjQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and BlingDoug Neiner
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.jsBert Wijnants
 
Using Objects to Organize your jQuery Code
Using Objects to Organize your jQuery CodeUsing Objects to Organize your jQuery Code
Using Objects to Organize your jQuery CodeRebecca Murphey
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutesSimon Willison
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do MoreRemy Sharp
 
Drupal Development
Drupal DevelopmentDrupal Development
Drupal DevelopmentJeff Eaton
 
A New Baseline for Front-End Devs
A New Baseline for Front-End DevsA New Baseline for Front-End Devs
A New Baseline for Front-End DevsRebecca Murphey
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009Remy Sharp
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQueryRemy Sharp
 
Managing State in Single Page WebApps with Ember.js
Managing State in Single Page WebApps with Ember.jsManaging State in Single Page WebApps with Ember.js
Managing State in Single Page WebApps with Ember.jsMark Mansour
 
Dr. Strangelove or: How I learned to stop worrying and love HTML, CSS and Jav...
Dr. Strangelove or: How I learned to stop worrying and love HTML, CSS and Jav...Dr. Strangelove or: How I learned to stop worrying and love HTML, CSS and Jav...
Dr. Strangelove or: How I learned to stop worrying and love HTML, CSS and Jav...RobotDeathSquad
 

La actualidad más candente (20)

Sane Async Patterns
Sane Async PatternsSane Async Patterns
Sane Async Patterns
 
A Short Introduction To jQuery
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuery
 
jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & CompressionjQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & Compression
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
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
 
Client-side MVC with Backbone.js
Client-side MVC with Backbone.js Client-side MVC with Backbone.js
Client-side MVC with Backbone.js
 
jQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and BlingjQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and Bling
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
Using Objects to Organize your jQuery Code
Using Objects to Organize your jQuery CodeUsing Objects to Organize your jQuery Code
Using Objects to Organize your jQuery Code
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
Drupal Development
Drupal DevelopmentDrupal Development
Drupal Development
 
A New Baseline for Front-End Devs
A New Baseline for Front-End DevsA New Baseline for Front-End Devs
A New Baseline for Front-End Devs
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQuery
 
jQuery
jQueryjQuery
jQuery
 
Managing State in Single Page WebApps with Ember.js
Managing State in Single Page WebApps with Ember.jsManaging State in Single Page WebApps with Ember.js
Managing State in Single Page WebApps with Ember.js
 
Dr. Strangelove or: How I learned to stop worrying and love HTML, CSS and Jav...
Dr. Strangelove or: How I learned to stop worrying and love HTML, CSS and Jav...Dr. Strangelove or: How I learned to stop worrying and love HTML, CSS and Jav...
Dr. Strangelove or: How I learned to stop worrying and love HTML, CSS and Jav...
 

Similar a JavaScript for Single Page Apps

JavaScript!
JavaScript!JavaScript!
JavaScript!RTigger
 
Rich Internet Applications con JavaFX e NetBeans
Rich Internet Applications  con JavaFX e NetBeans Rich Internet Applications  con JavaFX e NetBeans
Rich Internet Applications con JavaFX e NetBeans Fabrizio Giudici
 
Implementing Awesome: An HTML5/CSS3 Workshop
Implementing Awesome: An HTML5/CSS3 WorkshopImplementing Awesome: An HTML5/CSS3 Workshop
Implementing Awesome: An HTML5/CSS3 WorkshopShoshi Roberts
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScriptbensmithett
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Chris Alfano
 
Using jQuery to Extend CSS
Using jQuery to Extend CSSUsing jQuery to Extend CSS
Using jQuery to Extend CSSChris Coyier
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 
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 Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue AdventureAllegient
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando dias
 
Unobtrusive javascript with jQuery
Unobtrusive javascript with jQueryUnobtrusive javascript with jQuery
Unobtrusive javascript with jQueryAngel Ruiz
 
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
 
Building DSLs with Groovy
Building DSLs with GroovyBuilding DSLs with Groovy
Building DSLs with GroovySten Anderson
 
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.GeeksLab Odessa
 
Everything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebEverything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebJames Rakich
 
前端MVC之BackboneJS
前端MVC之BackboneJS前端MVC之BackboneJS
前端MVC之BackboneJSZhang Xiaoxue
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
 
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridasFrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridasLoiane Groner
 

Similar a JavaScript for Single Page Apps (20)

JavaScript!
JavaScript!JavaScript!
JavaScript!
 
Rich Internet Applications con JavaFX e NetBeans
Rich Internet Applications  con JavaFX e NetBeans Rich Internet Applications  con JavaFX e NetBeans
Rich Internet Applications con JavaFX e NetBeans
 
Implementing Awesome: An HTML5/CSS3 Workshop
Implementing Awesome: An HTML5/CSS3 WorkshopImplementing Awesome: An HTML5/CSS3 Workshop
Implementing Awesome: An HTML5/CSS3 Workshop
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
 
Using jQuery to Extend CSS
Using jQuery to Extend CSSUsing jQuery to Extend CSS
Using jQuery to Extend CSS
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
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 Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
Unobtrusive javascript with jQuery
Unobtrusive javascript with jQueryUnobtrusive javascript with jQuery
Unobtrusive javascript with jQuery
 
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)
 
Building DSLs with Groovy
Building DSLs with GroovyBuilding DSLs with Groovy
Building DSLs with Groovy
 
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
 
Everything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebEverything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the Web
 
前端MVC之BackboneJS
前端MVC之BackboneJS前端MVC之BackboneJS
前端MVC之BackboneJS
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridasFrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
 
Html5 For Jjugccc2009fall
Html5 For Jjugccc2009fallHtml5 For Jjugccc2009fall
Html5 For Jjugccc2009fall
 

Último

Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Último (20)

Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

JavaScript for Single Page Apps

  • 2. HELLO my name is @AARONIUS · AARONHARDY.COM Software Engineer, Adobe Digital Marketing Suite We enable management, measurement, execution, and optimization of digital advertising and marketing.
  • 5. TabBar CurrencyFormatter EventDispatcher List ArrayCollection DataGroup DragManager ToggleButtonBar Panel NumericStepper Slider ViewStack Accordion Bindable DataGrid Effects
  • 6. AccountInfo (Group) Header (Group) Menu (ToggleButtonBar) Main Biography (Application) (Group) Artist (Group) Similar Artists (List) Music Discography (ViewStack) (DataGroup) Discussion (DataGroup)
  • 7. ? Links ? Basic Form Elements Div Image ? Bulleted Lists ? ? Span ?
  • 8. <html> <head> <script> buckets of vomit buckets of vomit buckets of vomit buckets of vomit buckets of vomit </script> </head> <body> buckets of vomit buckets of vomit buckets of vomit buckets of vomit buckets of vomit </body> </html>
  • 9. IDEs • MVC • Widgets/Components • Dependency management • DOM manipulation • Templating • Testing • Utilities • JavaScript + HTML + CSS
  • 13. jquery underscore.js backbone.js requirejs All MIT license (+ other options) and on GitHub
  • 15. Never build large apps The secret to building large apps is NEVER build large apps. Break up your applications into small pieces. Then, assemble those testable, bite-sized pieces into your big application. – Justin Meyer
  • 16. Admit what you don’t know The key is to acknowledge from the start that you have no idea how this will grow. When you accept that you don’t know everything, you begin to design the system defensively. – Nicholas Zakas
  • 17. Add "use strict"; before code or at beginning of function to help decrapify your code. • Eliminates pitfalls by raising errors • Improves JS engine optimization • Prohibits syntax reserved for future JS versions * Won’t be used in examples to save slide space
  • 18. JSLint or JSHint. • Run in IDE. • Run on command line. • Run in build scripts. • Copy-paste into web interface. • Use flags for configuration.
  • 19. General community style: • Names of directories and files shall be lowercase and hyphens shall be used to separate words. • Variable names shall be camelCase. • Classes (functions) to be instantiated shall start with a capital letter. • Members intended to be private shall be prefixed with an underscore.
  • 20. Loose typing == more testing! • JsTestDriver • QUnit • Jasmine • Vows • …and bucket-loads more
  • 22. • Not an application framework! • Abstracts the DOM • Style • Position • Behavior • Animation • Creation/Removal • AJAX requests • Utilities for objects, arrays, strings, etc. • “Framework” for UI widgets and utilities
  • 23. Usage by Top Sites http://trends.builtwith.com/javascript/jQuery
  • 24. • Performance-tuned servers • Allows browser to have more concurrent connections • Cross-site caching
  • 25. Toolbox of array, object, and function manipulation utilities. var names = _.pluck( [{name: 'moe', age: 40}, {name: 'larry', age: 50}], 'name'); var person = _.extend({name: 'moe'}, {age: 50}); var uniques = _.union([1, 2, 3], [101, 2, 10], [2, 1]); And bucket-loads more…
  • 26. Old-school var tweetTemplate = '' + '<div>' + '<div style="float: left">' + '<img src="' + avatar + '"/>' + '</div>' + '<div style="margin-left: 60px">' + '<p>' + username + '</p>' + '<p>' + text + '</p>' + '</div>' + '<div>';
  • 27. Old-school • HTML buried in logic (hard to re-skin) • Concatenation/escaping takes time and is error-prone • IDE might not code-hint or color-code properly • Boring and tedious
  • 28. New-school <script type="text/template" id="tweet-template"> <div> <div style="float: left"> <img src="<%= avatar %>"/> </div> <div style="margin-left: 60px"> <p><%= username %></p> <p><%= text %></p> </div> </div> </script> <script> var tweet = {avatar: 'aaronius.jpg', alias: '@Aaronius', text: 'Honey roasted peanuts rock my sox.'}; var template = _.template($('#tweet-template').html()); var html = template(tweet); var element = $(html); </script>
  • 29. Mustache.js • Handlebars.js • Jade • Eco • …and bucket-loads more. Underscore.js templating is one of the best (though not as full-featured) and is a dependency of Backbone.js. You can use any templating engine.
  • 31. Object A passes a function to object B and tells object B to call the function when X occurs. When X occurs, object B calls the function.
  • 33. Dad: var baby = new Baby(); var changeDeuceTrap = function() { baby.deuces = 0; baby.cakeHoleShut = true; }; baby.on('turdDropped', changeDeuceTrap); Baby: this.trigger('turdDropped');
  • 34. Mix into any object or class var baby = new Baby(); baby = _.extend(baby, Backbone.Events); baby.on(…); baby.trigger(…);
  • 36. Plain Old JavaScript Object var book = { title: 'A Tale of Two Cities', author: 'Charles Dickens', genre: 'historical', publisher: 'Chapman & Hall' }; How can another object know when one of book’s values changes? It can’t!
  • 37. Eventful Backbone.Model var book = new Backbone.Model({ title: 'A Tale of Two Cities', author: 'Charles Dickens', genre: 'historical', publisher: 'Chapman & Hall' }); book.on('change:genre', function(model, genre) { alert('genre for ' + model.get('title') + ' changed to ' + genre); }); book.set({genre: 'social criticism'});
  • 38. Extending Backbone.Model var Book = Backbone.Model.extend({ …custom model logic… }); var book = new Book({ title: 'A Tale of Two Cities', author: 'Charles Dickens', publisher: 'Chapman & Hall' });
  • 39. Persistence HTTP Method URL Action GET /books Retrieve all books GET /books/10 Retrieve book with id == 10 POST /books Add a book PUT /books/10 Update book with id == 10 DELETE /books/10 Delete book with id == 10
  • 40. Persistence (save) var Book = Backbone.Model.extend({ urlRoot: '/books' }); var book = new Book({title: 'The Tragedy of the Commons'}); book.save();
  • 41. Persistence (fetch) var Book = Backbone.Model.extend({ urlRoot: '/books' }); var book = new Book({id: 2}); var fetchSuccess = function() { alert(book.get('title')); }; book.fetch({success: fetchSuccess});
  • 42.
  • 43.
  • 44. MODEL layersOpen characterOpen magicWandOpen colorOpen infoOpen
  • 45. And more… • Validation • Extracting native object • Determine which attributes have changed • And bucket-loads more…
  • 47. Plain Old JavaScript Array: var books = [book1, book2, book3, book4]; How can another object know when an item is added or removed from this array? It can’t!
  • 48. var goodEarth = new Backbone.Model(); var books = new Backbone.Collection(); books.on('add', function(book, books, options) { alert('Book ' + book.get('title') + ' added at index ' + options.at + '. ' + 'The collection now contains ' + books.length + ' models.'); }; books.on('remove', function(book, books, options) { alert('Book ' + book.get('title') + ' removed from index ' + options.index); } books.add(goodEarth); books.remove(goodEarth);
  • 49. Event Pass-through var books = new Backbone.Collection([taleOfTwoCities, goodEarth]); books.on('change:title', function(book, title) { alert('Book title changed from ' + book.previous('title') + ' to ' + title); }); goodEarth.set({title: 'Good Earth, The'});
  • 50. Persistence (fetch) var Books = Backbone.Collection.extend({ model: Book, url: '/books' }); var books = new Books(); books.fetch();
  • 51. Underscore mix-ins var titles = books.pluck('title'); each(), reduce(), find(), filter(), reject(), shuffle(), without() and bucket-loads more!
  • 52. And more… • Sorting • Resetting • Retrieving model by id or index • Custom loading and parsing • …and bucket-loads more!
  • 54.
  • 55.
  • 56. Stats Model Tweet Model Tweet Tweet Tweet Collection Tweet Tweet Tweet Tweet
  • 57. var Tweet = Backbone.Model.extend({ …view logic… }); var TweetRow = Backbone.View.extend({ …view logic… }); var tweet = new Tweet({ avatar: 'aaronius.jpg', alias: '@Aaronius', text: 'Honey roasted peanuts rock my sox.' }); var row = new TweetRow({ model: tweet });
  • 58. <script type="text/template" id="tweet-row-template"> <div style="float: left"><img src="<%= avatar %>"/></div> <div style="margin-left: 60px"> <p><%= username %></p> <p><%= text %></p> </div> </script> var TweetRow = Backbone.View.extend({ _template: _.template($('#tweet-row-template').html()), initialize: function() { this.render(); }, render: function() { this.$el.html(this._template(this.model.toJSON())); return this; } });
  • 62. Must we be restricted to working inside a few monstrous spaghetti files? NO! Then why are they so prevalent? • “That’s the way JavaScript has been done in the past.” • “Loading many JavaScript files requires many HTTP requests resulting in longer load times.” • “Dependency management is hard in JavaScript.” We can do better!
  • 63. Old school // What are the dependencies here!? // What if a new employee had to re-order for some reason!? <script src="script3.js"></script> <script src="script1.js"></script> <script src="script7.js"></script> <script src="script6.js"></script> <script src="script4.js"></script> <script src="script5.js"></script> <script src="script9.js"></script> <script src="script8.js"></script> <script src="script10.js"></script> <script src="script2.js"></script>
  • 64. script8.js script4.js script3.js script1.js script2.js script10.js script7.js script9.js script5.js script6.js
  • 65. ServerJS  CommonJS  Module  Async module definition (AMD)  RequireJS
  • 66. book.js define({ title: "My Sister's Keeper", publisher: "Atria" }); bookshelf.js define([ 'book' ], function(book) { return { listBook: function() { alert(book.title); } }; });
  • 67. index.html <!DOCTYPE html> <html> <head> <title>RequireJS Example</title> <script data-main="js/main“ src="js/libs/require.js"></script> </head> <body/> </html> main.js require([ 'bookshelf' ], function(bookshelf) { bookshelf.listBook(); });
  • 68. templates/book.tpl.html <span class="label">Title:</span> <span class="value"><%= title %></span><br/> <span class="label">Author:</span> <span class="value"><%= author %></span><br/> <span class="label">Genre:</span> <span class="value"><%= genre %></span>
  • 69. book-view.js define([ 'backbone', 'underscore', 'text!templates/book.tpl.html' ], function(Backbone, _, template) { return Backbone.View.extend({ _template: _.template(template), initialize: function() { this.render(); }, render: function() { var nativeBook = this.model.toJSON(); var html = this._template(nativeBook); this.$el.html(html); return this; } }); });
  • 70. bookshelf-view.js define([ 'backbone', 'underscore', 'book-view' ], function(Backbone, _, BookView) { return Backbone.View.extend({ … create child book views as necessary … … new BookView() … }); });
  • 72. Thank you! @Aaronius aaronhardy.com
  • 73. encore (stuff I fit in if time allows)
  • 74. Persistence with custom url pattern: var Book = Backbone.Model.extend({ urlRoot: '/books', url: function() { return this.urlRoot + '?id=' + this.get('id'); } }) var book = new Book({id: 2}); book.fetch();
  • 75. var App = Backbone.View.extend({ initialize: function() { this.render(); }, render: function() { var tweet = new Tweet({ avatar: 'avatar.jpg', username: '@Aaronius', text: 'Honey roasted peanuts rock my sox.' }); var row = new TweetRow({ model: tweet }); this.$el.append(row.$el); return this; } }); var app = new App({el: $('body')});
  • 76. var DocumentView = Backbone.View.extend({ events: { "dblclick" : "open", "click .icon.doc" : "select", "mouseover .title .date" : "showTooltip" }, render: function() { ... }, open: function() { ... }, select: function() { ... }, showTooltip: function() { ... }, });
  • 77. <script type="text/javascript"> $(function() { var AppRouter = Backbone.Router.extend({ routes: { "shirt/id/:id": "showShirt" }, showShirt: function(id) { alert('Show shirt with id ' + id); } }); var appRouter = new AppRouter(); Backbone.history.start(); }); </script> <a href="#shirt/id/5">Shirt with id of 5</a><br> <a href="#shirt/id/10">Shirt with id of 10</a><br> <a href="#shirt/id/15">Shirt with id of 15</a><br>
  • 78. <script type="text/javascript"> $(function() { var AppRouter = Backbone.Router.extend({ routes: { ":product/:attribute/:value": "showProduct" }, showProduct: function(product, attribute, value) { alert('Show ' + product + ' where ' + attribute + ' = ' + value + '.'); } }); var appRouter = new AppRouter(); Backbone.history.start(); }); </script> <a href="#shoe/size/12">Size 12 shoes</a><br> <a href="#shirt/id/5">Shirt with id of 5</a><br> <a href="#hat/color/black">Black hats</a>
  • 79. var AppRouter = Backbone.Router.extend({ routes: { ":product/:attribute/:value": "showProduct" } }); var MyView = Backbone.View.extend({ initialize: function(options) { options.router.on('route:showProduct', this._showProduct); } _showProduct: function(product, attribute, value) { alert('Update to show ' + product + ' where ' + attribute + ' = ' + value + '.'); } }); var appRouter = new AppRouter(); var view = new MyView({router: appRouter}); Backbone.history.start();
  • 80. Fragment Identifier • Using URLs in unintended ways • Dependent on JavaScript • Controversial • Used by many popular sites pushState aka HTML5 History • The bee’s knees • Allows changing of core url without changing pages • Long-term solution • Browser support lacking (guess which browser)

Notas del editor

  1. Is the product part of a larger suite? What is the rest of the suite using?How hard would it be for us to refactor to another library?Is it a fad or popular for good reason?What’s the community like?How open-source is it?What’s the learning curve?How easy is it to hire developers that want to work with it?
  2. How to handle many templates.
  3. MV* Application FrameworkNon-prescriptive1200 lines of code (with comments)Extensible
  4. Like EventDispatcher
  5. Like [Bindable] model
  6. Like ArrayCollection
  7. Similar to PropertyChangeEvent
  8. Case Study: Update view when tweet is sent
  9. Case Study: Update view when tweet is sent
  10. Significance of this.el
  11. Kevin Dangoor