SlideShare une entreprise Scribd logo
1  sur  34
Télécharger pour lire hors ligne
Backbone
A Brief Introduction
Thursday, August 8, 13
What is Backbone
• Gives structure to web applications
• by providing models with key-value binding
and custom events,
• collections with a rich API of enumerable
functions,
• views with declarative event handling,
• and connects it all to your existing API over a
RESTful JSON interface.
Thursday, August 8, 13
Backbone IS NOT
• It is not a framework
Thursday, August 8, 13
Structure
Thursday, August 8, 13
Dependencies
• jQuery (or Zepto)
• underscore (or Lo-Dash)
• JSON2.js (for old browsers such as IE)
Thursday, August 8, 13
Components
• Backbone.Events
• Backbone.Model
• Backbone.Collecti
on
• Backbone.View
• Backbone.Router
• Backbone.History
• Backbone.sync
Thursday, August 8, 13
Event Driven
Events
(mixin)
Router
History
View
Collection
Model
Thursday, August 8, 13
Events
object = {}
_.extend object, Backbone.Events
object.on "alert", (msg) ->
alert "Triggered " + msg
object.trigger "alert", "an
event"
Thursday, August 8, 13
MVC
View
Controller*
Model &
Collection
Control
Event
Thursday, August 8, 13
Controller
• NO Backbone.Controller
• Controller is a concept. Some put
controller logic in Router, some put inView
• Controller can be extracted out as
collection of functions
Thursday, August 8, 13
Controller
Responsibilities
• Construct views
• Sync models/collections through API
• Connect models/collections with views
Thursday, August 8, 13
Controller Sample
todos = new TodosCollection
todosController =
index: ->
view = new TodoIndex collection: todos
App.layout.content.show view.render()
show: (id) ->
todo = todos.get(id)
view = new TodoShow model: todo
App.layout.content.show view.render()
Thursday, August 8, 13
Interaction
View
Controller*
Router &
History
Manipulate
DOM Tree
DOM Events
Control
Event
Update URL
URL Change
Back/Forward
Thursday, August 8, 13
Sync
Model & Collection
Backbone.sync
HTML5
localStorage
$.ajax
CouchDB
Thursday, August 8, 13
Model & Collection
Thursday, August 8, 13
Model
• Key-value attributes
• Changes events via set method: "change",
"change[attribute]"
• JSON serialization
• Validation
Thursday, August 8, 13
Collection
• Array/Hash of Models
• underscore methods
• Collection events: "add", "remove",
"reset"
• Model events aggregation
• JSON serialization
Thursday, August 8, 13
class Todo extends Backbone.Model
class TodosCollection extends Backbone.Collection
model: Todo
todo = new Todo title: "Backbone", done: true
todos = new TodosCollection
todos.add todo
todos.add title: "Marionette", done: false
todo.on 'change:done', (model) ->
console.log model.toJSON()
todos.on 'add', (model, coll) ->
console.log "new item"
Code Sample
Thursday, August 8, 13
Backbone.sync
• Model persistence
• Signature sync(method, model, [options])
• method - CRUD
• model - model or collection to save
• options - callbacks, other options for
sync implementation (e.g. jQuery ajax
options)
Thursday, August 8, 13
sync implementations
• Bundled with RESTful API via jQuery.ajax
• jeromegn/Backbone.localStorage
• pyronicide/backbone.couchdb.js
Thursday, August 8, 13
View
Thursday, August 8, 13
DOM Manipulation
• View is attached to DOM via el attribute
• If el is not specified, it is created using
tagName, id, className
• Use render to setup the HTML in el
Thursday, August 8, 13
Code Sample
class TodoShow extends Backbone.View
tagName: 'li'
className: 'todo-item'
# It is good practices to use template instead,
# such as Handlebars
render: ->
context = model.toJSON()
@$el.html "<label><input type=”checkbox”> #{context.title}
</label>"
@ui = { label: @$('label'), input: @$('input') }
if @model.get('done')
@ui.input.prop 'checked', true
@ui.label.css 'text-decoration', 'line-through'
@ # It is a convention to return the view itself
Thursday, August 8, 13
Handle Model Events
class TodoShow extends Backbone.View
initialize: ->
# new methods in 1.0.0
# pay attention to events cycle
@listenTo @model, 'change', @render
...
Thursday, August 8, 13
Handle DOM Events
• events attribute
• {"event selector": "callback"}
Thursday, August 8, 13
Code Sample
class TodoShow extends Backbone.View
events:
'click label': 'toggle'
toggle: (e) ->
e.preventDefault()
e.stopPropagation()
isDone = !@model.get('done')
@model.set 'done', isDone
Thursday, August 8, 13
Router & History
Thursday, August 8, 13
Routes
• Router is auto registered when new
instance is created
• routes attribute
• {"url/pattern": "callback"}
• Start routing
# Use hash fragments
Backbone.history.start()
# or use HTML5 History API
# Backbone.history.start({pushState: true})
Thursday, August 8, 13
Code Sample
class TodoRouter extends Backbone.Router
routes:
'': 'activeTodos'
'all': 'allTodos'
activeTodos: ->
todosController.index(onlyActive: true)
allTodos: ->
todosController.index()
Thursday, August 8, 13
Further Readings
Thursday, August 8, 13
Documentation
• http://backbonejs.org/
• Developing Backbone.js Applications (free
ebook) http://addyosmani.github.io/
backbone-fundamentals/
Thursday, August 8, 13
Resources
• Sample: http://todomvc.com/architecture-
examples/backbone/
• https://github.com/jashkenas/backbone/wiki/
Extensions,-Plugins,-Resources
Thursday, August 8, 13
Marionette
• Application library based on Backbone
• Scale for large JavaScript applications
• Collection of common design, good
patterns and best practices
Thursday, August 8, 13
Marionette Resources
• http://marionettejs.com/
• Backbone.Marionette.js:A Gentle
Introduction ($17) https://leanpub.com/
marionette-gentle-introduction
• Building Backbone Plugins ($25) https://
leanpub.com/building-backbone-plugins
Thursday, August 8, 13

Contenu connexe

Tendances

BarCamp CR 2013 - Angularjs - Brian Salazar
BarCamp CR 2013 - Angularjs - Brian SalazarBarCamp CR 2013 - Angularjs - Brian Salazar
BarCamp CR 2013 - Angularjs - Brian Salazar
barcampcr
 
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresJavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
Robert Nyman
 
Spca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_librariesSpca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_libraries
NCCOMMS
 
Intro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptIntro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate Javascript
Andrew Lovett-Barron
 

Tendances (20)

Boosting Your Productivity, with Backbone & RactiveJS
Boosting Your Productivity, with Backbone & RactiveJS Boosting Your Productivity, with Backbone & RactiveJS
Boosting Your Productivity, with Backbone & RactiveJS
 
Drupal 8: Fields reborn
Drupal 8: Fields rebornDrupal 8: Fields reborn
Drupal 8: Fields reborn
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
BackboneJS Training - Giving Backbone to your applications
BackboneJS Training - Giving Backbone to your applicationsBackboneJS Training - Giving Backbone to your applications
BackboneJS Training - Giving Backbone to your applications
 
Backbonejs for beginners
Backbonejs for beginnersBackbonejs for beginners
Backbonejs for beginners
 
Backbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVCBackbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVC
 
Ractive js
Ractive jsRactive js
Ractive js
 
BarCamp CR 2013 - Angularjs - Brian Salazar
BarCamp CR 2013 - Angularjs - Brian SalazarBarCamp CR 2013 - Angularjs - Brian Salazar
BarCamp CR 2013 - Angularjs - Brian Salazar
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.js
 
Introduction to Ember
Introduction to EmberIntroduction to Ember
Introduction to Ember
 
Digesting jQuery
Digesting jQueryDigesting jQuery
Digesting jQuery
 
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresJavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
 
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
 
Spca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_librariesSpca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_libraries
 
Cocoahead index identifier
Cocoahead index identifierCocoahead index identifier
Cocoahead index identifier
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For Managers
 
Backbone js
Backbone jsBackbone js
Backbone js
 
The Hitchhikers Guide To Html5 Offline Strategies (+firefoxOS)
The Hitchhikers Guide To Html5 Offline Strategies (+firefoxOS)The Hitchhikers Guide To Html5 Offline Strategies (+firefoxOS)
The Hitchhikers Guide To Html5 Offline Strategies (+firefoxOS)
 
Getting Started With Google Wave Developlement
Getting Started With Google Wave DeveloplementGetting Started With Google Wave Developlement
Getting Started With Google Wave Developlement
 
Intro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptIntro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate Javascript
 

Similaire à Backbone intro

Backbone.js Simple Tutorial
Backbone.js Simple TutorialBackbone.js Simple Tutorial
Backbone.js Simple Tutorial
추근 문
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
rsnarayanan
 
2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx
Le Hung
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
Hjörtur Hilmarsson
 

Similaire à Backbone intro (20)

Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAE
 
Backbone.js Simple Tutorial
Backbone.js Simple TutorialBackbone.js Simple Tutorial
Backbone.js Simple Tutorial
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
 
2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx
 
Javascript Application Architecture with Backbone.JS
Javascript Application Architecture with Backbone.JSJavascript Application Architecture with Backbone.JS
Javascript Application Architecture with Backbone.JS
 
Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneAplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com Backbone
 
Prateek dayal backbonerails-110528024926-phpapp02
Prateek dayal backbonerails-110528024926-phpapp02Prateek dayal backbonerails-110528024926-phpapp02
Prateek dayal backbonerails-110528024926-phpapp02
 
Single Page Web Apps with Backbone.js and Rails
Single Page Web Apps with Backbone.js and RailsSingle Page Web Apps with Backbone.js and Rails
Single Page Web Apps with Backbone.js and Rails
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
 
Data visualization in python/Django
Data visualization in python/DjangoData visualization in python/Django
Data visualization in python/Django
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
BACKBONE.JS & UNDERSCORE.JS
BACKBONE.JS & UNDERSCORE.JSBACKBONE.JS & UNDERSCORE.JS
BACKBONE.JS & UNDERSCORE.JS
 
Backbone the Good Parts
Backbone the Good PartsBackbone the Good Parts
Backbone the Good Parts
 
An introduction to Ember.js
An introduction to Ember.jsAn introduction to Ember.js
An introduction to Ember.js
 
Backbone Basics with Examples
Backbone Basics with ExamplesBackbone Basics with Examples
Backbone Basics with Examples
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Backbone
BackboneBackbone
Backbone
 
Creating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSCreating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JS
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
 

Dernier

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
Safe Software
 
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
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
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
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Dernier (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
 
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
 
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
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
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
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation 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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Backbone intro