SlideShare a Scribd company logo
1 of 10
Backbone.js 
Models & Views in JavaScript 
János Rusiczki 
SoftVision
Backbone.js 
● Problem? 
● Solution! 
● Model 
● Collection 
● Template 
● View 
● API synchronization
Problem? 
● Standard JavaScript libraries let you 
quickly build apps 
but... 
● You get no structure 
● Nested callbacks 
● Everything is tied to DOM elements
Solution = Backbone.js 
● Well structured 
– Models 
– Collections 
– Views 
● Lightweight 
● Deep linking (Routers) 
● Effortless API synchronization 
● Open source
Model 
"Models are the heart of any JavaScript 
application, containing the interactive data 
as well as a large part of the logic 
surrounding it: conversions, validations, 
computed properties, and access control." 
~ The creators of Backbone.js
Model 
Person = Backbone.Model.extend({ 
defaults: { name: "Newborn Baby", age: 0 }, 
validate: function(attributes) { 
if(attributes.name == "") { 
return "Really? A person without a name?"; 
} 
}, 
initialize: function() { 
this.bind("change:name", function() { 
console.log("New name: " + this.get("name")); 
}); 
} 
}); 
var person = new Person({ name: "John", age: 28 });
Collection 
var p1 = new Person({ name: "John", age: 25 }); 
var p2 = new Person(); 
var p2_name = p2.get("name"); // = Newborn Baby 
p2.set({ name: "Jane", age: p1.get("age") }); 
People = Backbone.Collection.extend({ 
model: Person, 
comparator: function(item) { 
return item.get("name"); 
} 
}); 
var twins = new People([ p1, p2 ]);
Template 
<script type="text/template" id="ti_template"> 
<label for="<%= elid %>"><%= textual %></label> 
<input type="text" id="<%= elid %>" /> 
</script> 
variables = { 
elid: "first_name", 
textual: "First Name" 
}; 
var template = 
_.template($("#ti_template").html(), variables );
View 
FirstName = Backbone.View.extend({ 
initialize: function() { 
this.render(); 
}, 
render: function() { 
// include last slide 
this.el.html(template); 
}, 
events: { 
"click": "clicked" 
}, 
clicked: function(event) { 
console.log("I heard a click..."); 
} 
});
API synchronization 
...with a RESTful API: 
Addresses = Backbone.Collection.extend({ 
Done! 
url: 'api/addresses' 
}); 
var addresses = new Addresses(); 
addresses.fetch();

More Related Content

What's hot

PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
 

What's hot (19)

Jquery image slider
Jquery image slider Jquery image slider
Jquery image slider
 
jQuery
jQueryjQuery
jQuery
 
Laravel, the right way - PHPConference 2016
Laravel, the right way - PHPConference 2016Laravel, the right way - PHPConference 2016
Laravel, the right way - PHPConference 2016
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Php update and delet operation
Php update and delet operationPhp update and delet operation
Php update and delet operation
 
Velocity 101 in Cascade Server CMS, by Penny Harding
Velocity 101 in Cascade Server CMS, by Penny HardingVelocity 101 in Cascade Server CMS, by Penny Harding
Velocity 101 in Cascade Server CMS, by Penny Harding
 
Jquery for post a form
Jquery for post a formJquery for post a form
Jquery for post a form
 
CSS3 and jQuery
CSS3 and jQueryCSS3 and jQuery
CSS3 and jQuery
 
PHP Lecture 4 - Working with form, GET and Post Methods
PHP Lecture 4 - Working with form, GET and Post MethodsPHP Lecture 4 - Working with form, GET and Post Methods
PHP Lecture 4 - Working with form, GET and Post Methods
 
Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
Javascript - Beyond-jQuery
 
Jqueryppt (1)
Jqueryppt (1)Jqueryppt (1)
Jqueryppt (1)
 
Closure
ClosureClosure
Closure
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
State of jQuery and Drupal
State of jQuery and DrupalState of jQuery and Drupal
State of jQuery and Drupal
 
CSS3 vs jQuery
CSS3 vs jQueryCSS3 vs jQuery
CSS3 vs jQuery
 
Utilising the data attribute
Utilising the data attributeUtilising the data attribute
Utilising the data attribute
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Handout for Metadata for Visual Resources
Handout for Metadata for Visual ResourcesHandout for Metadata for Visual Resources
Handout for Metadata for Visual Resources
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
 

Viewers also liked (6)

What is crm
What is crmWhat is crm
What is crm
 
Crm+ net suite v1.2
Crm+ net suite v1.2Crm+ net suite v1.2
Crm+ net suite v1.2
 
Ik tak, eskuntza aireberritzeko aitzakia
Ik tak, eskuntza aireberritzeko aitzakiaIk tak, eskuntza aireberritzeko aitzakia
Ik tak, eskuntza aireberritzeko aitzakia
 
Cash handling smart safes
Cash handling smart safesCash handling smart safes
Cash handling smart safes
 
Distribution on erp
Distribution on erpDistribution on erp
Distribution on erp
 
Vogue Analysis - Amy Davey
Vogue Analysis - Amy DaveyVogue Analysis - Amy Davey
Vogue Analysis - Amy Davey
 

Similar to Janos Rusiczki - Backbone.js - Models & views in JavaScript

Jython: Python para la plataforma Java (EL2009)
Jython: Python para la plataforma Java (EL2009)Jython: Python para la plataforma Java (EL2009)
Jython: Python para la plataforma Java (EL2009)
Leonardo Soto
 
Jython: Python para la plataforma Java (JRSL 09)
Jython: Python para la plataforma Java (JRSL 09)Jython: Python para la plataforma Java (JRSL 09)
Jython: Python para la plataforma Java (JRSL 09)
Leonardo Soto
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
Hjörtur Hilmarsson
 

Similar to Janos Rusiczki - Backbone.js - Models & views in JavaScript (20)

Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneAplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com Backbone
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
 
Experience Manager 6 Developer Features - Highlights
Experience Manager 6 Developer Features - HighlightsExperience Manager 6 Developer Features - Highlights
Experience Manager 6 Developer Features - Highlights
 
Jython: Python para la plataforma Java (EL2009)
Jython: Python para la plataforma Java (EL2009)Jython: Python para la plataforma Java (EL2009)
Jython: Python para la plataforma Java (EL2009)
 
Jython: Python para la plataforma Java (JRSL 09)
Jython: Python para la plataforma Java (JRSL 09)Jython: Python para la plataforma Java (JRSL 09)
Jython: Python para la plataforma Java (JRSL 09)
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
 
Backbone js in drupal core
Backbone js in drupal coreBackbone js in drupal core
Backbone js in drupal core
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
前端MVC之BackboneJS
前端MVC之BackboneJS前端MVC之BackboneJS
前端MVC之BackboneJS
 
Specification-Driven Development of REST APIs by Alexander Zinchuk
Specification-Driven Development of REST APIs by Alexander Zinchuk   Specification-Driven Development of REST APIs by Alexander Zinchuk
Specification-Driven Development of REST APIs by Alexander Zinchuk
 
Lecture 6: Client Side Programming 2
Lecture 6: Client Side Programming 2Lecture 6: Client Side Programming 2
Lecture 6: Client Side Programming 2
 
Tools and Projects Dec 2018 Edition
Tools and Projects Dec 2018 EditionTools and Projects Dec 2018 Edition
Tools and Projects Dec 2018 Edition
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
 
JavaScript Abstraction
JavaScript AbstractionJavaScript Abstraction
JavaScript Abstraction
 
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
 
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
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
How to React Native
How to React NativeHow to React Native
How to React Native
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

Janos Rusiczki - Backbone.js - Models & views in JavaScript

  • 1. Backbone.js Models & Views in JavaScript János Rusiczki SoftVision
  • 2. Backbone.js ● Problem? ● Solution! ● Model ● Collection ● Template ● View ● API synchronization
  • 3. Problem? ● Standard JavaScript libraries let you quickly build apps but... ● You get no structure ● Nested callbacks ● Everything is tied to DOM elements
  • 4. Solution = Backbone.js ● Well structured – Models – Collections – Views ● Lightweight ● Deep linking (Routers) ● Effortless API synchronization ● Open source
  • 5. Model "Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control." ~ The creators of Backbone.js
  • 6. Model Person = Backbone.Model.extend({ defaults: { name: "Newborn Baby", age: 0 }, validate: function(attributes) { if(attributes.name == "") { return "Really? A person without a name?"; } }, initialize: function() { this.bind("change:name", function() { console.log("New name: " + this.get("name")); }); } }); var person = new Person({ name: "John", age: 28 });
  • 7. Collection var p1 = new Person({ name: "John", age: 25 }); var p2 = new Person(); var p2_name = p2.get("name"); // = Newborn Baby p2.set({ name: "Jane", age: p1.get("age") }); People = Backbone.Collection.extend({ model: Person, comparator: function(item) { return item.get("name"); } }); var twins = new People([ p1, p2 ]);
  • 8. Template <script type="text/template" id="ti_template"> <label for="<%= elid %>"><%= textual %></label> <input type="text" id="<%= elid %>" /> </script> variables = { elid: "first_name", textual: "First Name" }; var template = _.template($("#ti_template").html(), variables );
  • 9. View FirstName = Backbone.View.extend({ initialize: function() { this.render(); }, render: function() { // include last slide this.el.html(template); }, events: { "click": "clicked" }, clicked: function(event) { console.log("I heard a click..."); } });
  • 10. API synchronization ...with a RESTful API: Addresses = Backbone.Collection.extend({ Done! url: 'api/addresses' }); var addresses = new Addresses(); addresses.fetch();