SlideShare a Scribd company logo
1 of 33
Download to read offline
Backbone.js in a Php
       Environment
         Midwest Php – March 2, 2013

Ken Harris – Sr. Developer, Telkonet.com
             Milwaukee, WI
Trends in Web Apps
           ●   Fatter Clients
           ●   Desktop style apps
           ●   Lots of Javascript
           ●   Lots of CSS
           ●   Requires structure




03/03/13                        Midwest Php   2
Backbone.js
 ●   Framework for client side Javascript

 ●   Organize and structure large JS applications

 ●   Lightweight – only 4kb minified

 ●   Becoming popular

03/03/13                 Midwest Php                3
Backbone.js
 ●   Agnostic & very flexible
 ●   Mixes well with other frameworks & modules
 ●   Lots of ways to do things
 ●   Two Backbone apps may look quite different
 ●   Especially good for migrating an app from
     server side to client side rendering



03/03/13                 Midwest Php              4
Backbone components
           ●   events
           ●   models
           ●   collections
           ●   views
           ●   routers




03/03/13                     Midwest Php   6
MVC Design Pattern?
 ●   Backbone is not classic MVC
 ●   Model – Data objects for resources
 ●   View – Like a view controller. Control and
     render templates.
 ●   Templates – Correspond to MVC view. Can use
     template facility of choice. Get rendered by the
     view.
 ●   Routers – Like Rails routes.

03/03/13                 Midwest Php                    7
Requirements

           ●   Underscore.js – support library
               ●   Especially for collections

           ●   Jquery or Zepto
               ●   Uses jquery.ajax to access server DB

           ●   Json2.js for RESTful persistence

03/03/13                       Midwest Php                8
Booklist Example




03/03/13         Midwest Php   9
Booklist Example




03/03/13         Midwest Php   10
Backbone.Events
 ●   Mixin module – can be added to any object
 ●   Bind, trigger, and unbind
 ●   Events are strings (eg. “change”)
 ●   Trigger can pass arguments
 ●   Changes to data models can trigger automatic
     refresh of views



03/03/13                 Midwest Php                11
Backbone.Events
var object = {};
_.extend(object, Backbone.Events);

object.bind(“poke”, function(msg) {
  alert(“Just got poked: “+msg); });

object.trigger(“poke”, “hello...”);




03/03/13                 Midwest Php   12
Backbone.Model
 ●   Heart of the application
 ●   Data + logic (conversions, validations,
     computed properties, access control, …)
 ●   Create new model by extending
     Backbone.Model
 ●   Create instance with new



03/03/13                 Midwest Php           13
Backbone.Model

var Book = Backbone.Model.extend({
       initialize: function() { // do something
                         },
       defaults: { loc : '', title : '', author : ''},
       urlRoot: '/booklist/db.php/'
  });

var book = new Book;


 03/03/13                    Midwest Php                 14
Backbone.Model
 ●   Properties
      ●    model.id – unique server id
      ●    model.cid – client id
      ●    model.defaults – default attribute values
 ●   Methods
      ●    Initialize() - called on creation
      ●    get(attribute) - getter
      ●    set(attributes) - setter
      ●    validate() - validation
03/03/13                           Midwest Php         15
Backbone.Model
 ●   More methods
       ●   fetch()
       ●   save()
       ●   destroy()
       ●   toJSON()
 ●   Events
       ●   change, destroy, error
       ●   change event can be bound to render a view

03/03/13                       Midwest Php              16
Server Interaction
 ●   Makes RESTful calls to server
 ●   CRUD (create,read,update,delete)
 ●   Create → POST      server/collection/id
 ●   Read   → GET       server/model/id
 ●   Update → PUT
 ●   Delete → DELETE
 ●   Data passed using JSON encoding

03/03/13                Midwest Php            17
Example Requests
●   METHOD host/table/id
●   GET http://myserver/customer/16
●   GET http://myserver/salesclass
●   POST http://myserver/payment
●   DELETE http://myserver/appointment/2137




03/03/13               Midwest Php            18
Backbone.Collection


●   Ordered set of models

●   Can listen for events on any model

●   Create by extending Backbone.Collection

●   Create an instance usingPhp
 03/03/13               Midwest
                                new           19
Backbone.Collection

    var Booklist = Backbone.Collection.extend({
                   model : Book,
                   url : '/phpdemo/db.php'
            });

   var booklist = new Booklist;




03/03/13                          Midwest Php     20
Backbone.Collection

●   Methods
    ●      add()
    ●      remove()
    ●      get(id)
    ●      getByCid(cid)
    ●      Comparator – ordering function
    ●      Uses underscore.js for iteration methods


03/03/13                        Midwest Php           21
Backbone.View

   ●   Control piece that renders a view
   ●   Extend Backbone.View to create new view
   ●   Use new to create an instance
   ●   View.el – associated DOM element ($el)
   ●   initialize() function
   ●   render() function
   ●       $(selector) – locally scoped jQuery
03/03/13                       Midwest Php       22
Backbone.View




03/03/13       Midwest Php   23
underscore templates
 ●   _.template(“text”) returns a template function
     which merges properties into the text
 ●   Can embed template in dummy script tag
 ●   <script type=”text/template” id=”add_template”>
     </script>
 ●   <%= value %>     or <% javascript %>
 ●   template1 = _.template(“Hi <%= name %>”);
 ●   result = template1({ name : “bob”});
03/03/13                 Midwest Php                  24
add.tmpl




03/03/13     Midwest Php   25
Backbone.Router
 ●   Route client side pages and connect them to
     actions and events
 ●   Interfaces with hashchange events
 ●   Reacts to history navigation
 ●   Need to be aware of possible server
     interactions




03/03/13                 Midwest Php               26
Backbone.Router




03/03/13        Midwest Php   27
Booklist App
           ●   server
               ●   index.php
               ●   db.php

           ●   client-side
               ●   booklist.js
               ●   templates



03/03/13                         Midwest Php   28
index.php




03/03/13     Midwest Php   29
db.php




03/03/13    Midwest Php   30
db.php - continued




03/03/13          Midwest Php   31
db.php - continued




03/03/13          Midwest Php   32
References

   ●   Backbone.js Applications (Early Access), Addy Osmani, O'Reilly
       (2012)
   ●   Backbone.js: Basics & Beyond, Sarah Mei, Fluent2012 video,
       O'Reilly (2012)
   ●   Javascript Master Class, Douglas Crockford, O'Reilly Media (2009)
   ●   Javascript The Good Parts, Douglas Crockford, O'Reilly (2008)
   ●   Javascript Web Applications, Alex MacCaw, O'Reilly (2011)
   ●   Recipes With Backbone, Nick Gauthier and Chris Strom, eee_c
       (2012)
   ●   Secrets of Javascript Ninjas, John Resig and Bear Bibeault, Manning
       (2012)


03/03/13                           Midwest Php                             33
03/03/13   Midwest Php   34

More Related Content

Similar to Backbone midwestphp

Planbox Backbone MVC
Planbox Backbone MVCPlanbox Backbone MVC
Planbox Backbone MVCAcquisio
 
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan KrausHTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan KrausWomen in Technology Poland
 
FITC presents: Mobile & offline data synchronization in Angular JS
FITC presents: Mobile & offline data synchronization in Angular JSFITC presents: Mobile & offline data synchronization in Angular JS
FITC presents: Mobile & offline data synchronization in Angular JSFITC
 
Doctrine Project
Doctrine ProjectDoctrine Project
Doctrine ProjectDaniel Lima
 
Data Abstraction for Large Web Applications
Data Abstraction for Large Web ApplicationsData Abstraction for Large Web Applications
Data Abstraction for Large Web Applicationsbrandonsavage
 
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 applicationsJoseph Khan
 
React - The JavaScript Library for User Interfaces
React - The JavaScript Library for User InterfacesReact - The JavaScript Library for User Interfaces
React - The JavaScript Library for User InterfacesJumping Bean
 
Building Web Applications with Zend Framework
Building Web Applications with Zend FrameworkBuilding Web Applications with Zend Framework
Building Web Applications with Zend FrameworkPhil Brown
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalabilityTwinbit
 
Do something in 5 with gas 8-copy between databases
Do something in 5 with gas 8-copy between databasesDo something in 5 with gas 8-copy between databases
Do something in 5 with gas 8-copy between databasesBruce McPherson
 
JavascriptMVC: Another choice of web framework
JavascriptMVC: Another choice of web frameworkJavascriptMVC: Another choice of web framework
JavascriptMVC: Another choice of web frameworkAlive Kuo
 
Modern Perl Web Development with Dancer
Modern Perl Web Development with DancerModern Perl Web Development with Dancer
Modern Perl Web Development with DancerDave Cross
 
Drupal8 migrate
Drupal8 migrateDrupal8 migrate
Drupal8 migrateJohn Doyle
 

Similar to Backbone midwestphp (20)

Planbox Backbone MVC
Planbox Backbone MVCPlanbox Backbone MVC
Planbox Backbone MVC
 
Backbone 4.0
Backbone 4.0Backbone 4.0
Backbone 4.0
 
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan KrausHTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
 
FITC presents: Mobile & offline data synchronization in Angular JS
FITC presents: Mobile & offline data synchronization in Angular JSFITC presents: Mobile & offline data synchronization in Angular JS
FITC presents: Mobile & offline data synchronization in Angular JS
 
Doctrine Project
Doctrine ProjectDoctrine Project
Doctrine Project
 
Dust.js
Dust.jsDust.js
Dust.js
 
Data Abstraction for Large Web Applications
Data Abstraction for Large Web ApplicationsData Abstraction for Large Web Applications
Data Abstraction for Large Web Applications
 
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
 
React - The JavaScript Library for User Interfaces
React - The JavaScript Library for User InterfacesReact - The JavaScript Library for User Interfaces
React - The JavaScript Library for User Interfaces
 
Building Web Applications with Zend Framework
Building Web Applications with Zend FrameworkBuilding Web Applications with Zend Framework
Building Web Applications with Zend Framework
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalability
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Migrations
MigrationsMigrations
Migrations
 
Do something in 5 with gas 8-copy between databases
Do something in 5 with gas 8-copy between databasesDo something in 5 with gas 8-copy between databases
Do something in 5 with gas 8-copy between databases
 
JavascriptMVC: Another choice of web framework
JavascriptMVC: Another choice of web frameworkJavascriptMVC: Another choice of web framework
JavascriptMVC: Another choice of web framework
 
Node.js an Exectutive View
Node.js an Exectutive ViewNode.js an Exectutive View
Node.js an Exectutive View
 
Jsp
JspJsp
Jsp
 
Modern Perl Web Development with Dancer
Modern Perl Web Development with DancerModern Perl Web Development with Dancer
Modern Perl Web Development with Dancer
 
Drupal8 migrate
Drupal8 migrateDrupal8 migrate
Drupal8 migrate
 

Recently uploaded

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
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
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
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
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
#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
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
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
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
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
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
#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
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Backbone midwestphp

  • 1. Backbone.js in a Php Environment Midwest Php – March 2, 2013 Ken Harris – Sr. Developer, Telkonet.com Milwaukee, WI
  • 2. Trends in Web Apps ● Fatter Clients ● Desktop style apps ● Lots of Javascript ● Lots of CSS ● Requires structure 03/03/13 Midwest Php 2
  • 3. Backbone.js ● Framework for client side Javascript ● Organize and structure large JS applications ● Lightweight – only 4kb minified ● Becoming popular 03/03/13 Midwest Php 3
  • 4. Backbone.js ● Agnostic & very flexible ● Mixes well with other frameworks & modules ● Lots of ways to do things ● Two Backbone apps may look quite different ● Especially good for migrating an app from server side to client side rendering 03/03/13 Midwest Php 4
  • 5. Backbone components ● events ● models ● collections ● views ● routers 03/03/13 Midwest Php 6
  • 6. MVC Design Pattern? ● Backbone is not classic MVC ● Model – Data objects for resources ● View – Like a view controller. Control and render templates. ● Templates – Correspond to MVC view. Can use template facility of choice. Get rendered by the view. ● Routers – Like Rails routes. 03/03/13 Midwest Php 7
  • 7. Requirements ● Underscore.js – support library ● Especially for collections ● Jquery or Zepto ● Uses jquery.ajax to access server DB ● Json2.js for RESTful persistence 03/03/13 Midwest Php 8
  • 9. Booklist Example 03/03/13 Midwest Php 10
  • 10. Backbone.Events ● Mixin module – can be added to any object ● Bind, trigger, and unbind ● Events are strings (eg. “change”) ● Trigger can pass arguments ● Changes to data models can trigger automatic refresh of views 03/03/13 Midwest Php 11
  • 11. Backbone.Events var object = {}; _.extend(object, Backbone.Events); object.bind(“poke”, function(msg) { alert(“Just got poked: “+msg); }); object.trigger(“poke”, “hello...”); 03/03/13 Midwest Php 12
  • 12. Backbone.Model ● Heart of the application ● Data + logic (conversions, validations, computed properties, access control, …) ● Create new model by extending Backbone.Model ● Create instance with new 03/03/13 Midwest Php 13
  • 13. Backbone.Model var Book = Backbone.Model.extend({ initialize: function() { // do something }, defaults: { loc : '', title : '', author : ''}, urlRoot: '/booklist/db.php/' }); var book = new Book; 03/03/13 Midwest Php 14
  • 14. Backbone.Model ● Properties ● model.id – unique server id ● model.cid – client id ● model.defaults – default attribute values ● Methods ● Initialize() - called on creation ● get(attribute) - getter ● set(attributes) - setter ● validate() - validation 03/03/13 Midwest Php 15
  • 15. Backbone.Model ● More methods ● fetch() ● save() ● destroy() ● toJSON() ● Events ● change, destroy, error ● change event can be bound to render a view 03/03/13 Midwest Php 16
  • 16. Server Interaction ● Makes RESTful calls to server ● CRUD (create,read,update,delete) ● Create → POST server/collection/id ● Read → GET server/model/id ● Update → PUT ● Delete → DELETE ● Data passed using JSON encoding 03/03/13 Midwest Php 17
  • 17. Example Requests ● METHOD host/table/id ● GET http://myserver/customer/16 ● GET http://myserver/salesclass ● POST http://myserver/payment ● DELETE http://myserver/appointment/2137 03/03/13 Midwest Php 18
  • 18. Backbone.Collection ● Ordered set of models ● Can listen for events on any model ● Create by extending Backbone.Collection ● Create an instance usingPhp 03/03/13 Midwest new 19
  • 19. Backbone.Collection var Booklist = Backbone.Collection.extend({ model : Book, url : '/phpdemo/db.php' }); var booklist = new Booklist; 03/03/13 Midwest Php 20
  • 20. Backbone.Collection ● Methods ● add() ● remove() ● get(id) ● getByCid(cid) ● Comparator – ordering function ● Uses underscore.js for iteration methods 03/03/13 Midwest Php 21
  • 21. Backbone.View ● Control piece that renders a view ● Extend Backbone.View to create new view ● Use new to create an instance ● View.el – associated DOM element ($el) ● initialize() function ● render() function ● $(selector) – locally scoped jQuery 03/03/13 Midwest Php 22
  • 22. Backbone.View 03/03/13 Midwest Php 23
  • 23. underscore templates ● _.template(“text”) returns a template function which merges properties into the text ● Can embed template in dummy script tag ● <script type=”text/template” id=”add_template”> </script> ● <%= value %> or <% javascript %> ● template1 = _.template(“Hi <%= name %>”); ● result = template1({ name : “bob”}); 03/03/13 Midwest Php 24
  • 24. add.tmpl 03/03/13 Midwest Php 25
  • 25. Backbone.Router ● Route client side pages and connect them to actions and events ● Interfaces with hashchange events ● Reacts to history navigation ● Need to be aware of possible server interactions 03/03/13 Midwest Php 26
  • 26. Backbone.Router 03/03/13 Midwest Php 27
  • 27. Booklist App ● server ● index.php ● db.php ● client-side ● booklist.js ● templates 03/03/13 Midwest Php 28
  • 28. index.php 03/03/13 Midwest Php 29
  • 29. db.php 03/03/13 Midwest Php 30
  • 30. db.php - continued 03/03/13 Midwest Php 31
  • 31. db.php - continued 03/03/13 Midwest Php 32
  • 32. References ● Backbone.js Applications (Early Access), Addy Osmani, O'Reilly (2012) ● Backbone.js: Basics & Beyond, Sarah Mei, Fluent2012 video, O'Reilly (2012) ● Javascript Master Class, Douglas Crockford, O'Reilly Media (2009) ● Javascript The Good Parts, Douglas Crockford, O'Reilly (2008) ● Javascript Web Applications, Alex MacCaw, O'Reilly (2011) ● Recipes With Backbone, Nick Gauthier and Chris Strom, eee_c (2012) ● Secrets of Javascript Ninjas, John Resig and Bear Bibeault, Manning (2012) 03/03/13 Midwest Php 33
  • 33. 03/03/13 Midwest Php 34