SlideShare uma empresa Scribd logo
1 de 22
Show Some Spine!
    quick AJAX apps with Django&Backbone.js




w w w . c o m p e t e . c o m
An Overview of Our Evening’s Entertainment

     •    Ridiculously fast introduction to Backbone.js
     •    Building our AJAX app, a comedy in five acts
            •   Foundation
            •   The Django bits
            •   AJAX functionality
            •   Adding a delete feature and some validation
            •   Adding in editing

     •    Workshop: extend the app, ask questions, try something new




2   w w w . c o m p e t e . c o m
Backbone.js at 120mph




3   w w w . c o m p e t e . c o m
A First Date with Backbone.js

     •    AJAX apps turn into spaghetti pretty fast
     •    Javascript OOP is the Wild West of object systems
     •    Both of which spell doom for the tidy, reusable widgets
          imagined by your clever UI design
     •    So you might be thinking of borrowing someone else’s widget
          library
     •    But Backbone.js …
            •   … offers an MVC-ish framework for your front end
            •   … provides a basically sane object system
            •   … is small, reasonably easy to read, and has annotated source
            •   … works with a simple REST API to the backend server


4   w w w . c o m p e t e . c o m
A Discreet Aside on Architecture: MVC

     •    MVC, aka Model/View/Controller, aka Model/View promotes
          clarity of design
     •    Good design is the art of designing simple, flexible interfaces
            •   Components have well-defined responsibilities
            •   Components are intertwined only at interface boundaries

     •    MVC fits most applications, and supports good design
     •    The “Model” stores data and is responsible for mutating that
          data
     •    The “View” is a display of the Model’s data
     •    The “Controller” manages the interactions between Model and
          View


5   w w w . c o m p e t e . c o m
A Discreet Aside on Architecture: REST

     •    REST is short for “representational state transfer”
     •    REST refers to the use of the HTTP protocol features to
          construct an API
     •    For instance:
            •   An HTTP GET might fetch a data object from the server
            •   An HTTP POST could insert a new data object
            •   PUT can be used to update, and DELETE would naturally delete
            •   HTTP response codes can be used to indicate status of the request

     •    REST provides a very simple interface for exchanging data
     •    Contrast with SOAP or XML-RPC



6   w w w . c o m p e t e . c o m
Backbone’s Kinda MVC

     •    Models and Collections manage synchronization with the
          server layer
            •   Models represent a RESTful object
            •   Collections are a tidy way of managing lists of Models

     •    Views tie into the DOM and provide event handling semantics
     •    Routers can be used to map URLs or URL fragments onto
          functionality
     •    Templates can be lifted from the DOM, or prepared in the
          Javascript




7   w w w . c o m p e t e . c o m
Backbone’s Models

     •    Models use a REST API to sync with the server
            •   Model or Collection is provided with a URL
            •   Backbone uses URL + id to perform insert, delete, update, and fetch

     •    Models store their state in an “attributes” object
     •    Models raise a variety of events:
            •   change whenever the attributes change
            •   destroy when the model is destroyed
            •   error when a validation fails

     •    Additional methods and events can be added to the basic
          Model class to create a full-featured model



8   w w w . c o m p e t e . c o m
Backbone’s Collections

     •    Collections are a grouping of Models
     •    Collections can be arbitrary groups, or the complete set of
          available objects belonging to a particular Model subclass
     •    Collections raise a variety of events:
            •   reset whenever the whole collection is fetched or set
            •   will re-raise events from Models in the collection
            •   add and remove will fire when Models are added or removed

     •    Collections can define a comparator to enforce a sort order
            •   A comparator is a function that transforms a model into a sortable
                primitive
            •   The sort method itself can also be overridden



9   w w w . c o m p e t e . c o m
Backbone’s Views

      •    Views attach to a DOM element, which can be passed in, or
           can be created by the view
      •    Views provide an events object which maps event selectors to
           event handlers
      •    Views provide convenience features for working with JQuery
           to manipulate the DOM
      •    Views typically couple with a Collection, a Model, or both
      •    Views typically use a template or a DOM fragment to
           dynamically render themselves




10   w w w . c o m p e t e . c o m
Underscore templates & DOM fragments

      •    My basic setup customizes Underscore’s templates to have
           Django-like syntax
             •   {{ name }} interpolates the context variable name
             •   {% javascript = “spammy”; %} interpolates arbitrary Javascript into the
                 template – mostly useful for conditionals and loops and such

      •    Underscore templates are just Javascript strings
             •   Underscore compiles them into functions
             •   The functions accept a context object as an argument

      •    An alternate approach uses chunks of HTML hidden on the
           page and copied out with JQuery




11   w w w . c o m p e t e . c o m
Putting it all together




12   w w w . c o m p e t e . c o m
A Sample Backbone + Django App




13   w w w . c o m p e t e . c o m
Address: the app

      •    On Github: https://github.com/ggerrietts/vertebrate-django
      •    Built around a simple contact record: first, last, email
      •    Displays all contacts in the database
      •    Allows entry of a new contact
      •    Validates entered email addresses against a simple regex
      •    Enables deletion of contacts from the database
      •    Permits (inline) editing of the records in the database




14   w w w . c o m p e t e . c o m
App layout


         •    Common provides               /common
              framework elements                /common/static/{js,css,ext}
               •   Third-party components       /common/templates
                                                /common/views.py
               •   Master template
                                                /common/models.py
               •   “Library” classes        /address
         •    Address is our specific           /address/static/{js,css,img}
              application                       /address/templates
               •   App-specific media           /address/views.py
                                                /address/models.py
               •   App template(s)
                                                /address/admin.py
               •   Actual models & views
                                            /urls.py
                                            /settings.py


15   w w w . c o m p e t e . c o m
The app skeleton

      •    The app skeleton provides some frameworky infrastructure
      •    common/views.py maps a REST API onto Django models
      •    common/models.py has the default serialization mechanics
      •    common/static/ext has our third-party dependencies
      •    common/static/js has some customization code
      •    The skeleton also has a master template and master CSS file




16   w w w . c o m p e t e . c o m
Django-emails, the Django app

      •    address/models.py defines our contact record
      •    address/views.py is a simple REST-powered view
      •    address/templates/address/contact.html lays a foundation to
           build our app on
      •    This branch also adds a CSS file and some images
      •    We add an admin.py file to power up the admin interface
      •    And we builds out the global urls.py




17   w w w . c o m p e t e . c o m
Backbone-emails, a functional app

      •    All the interesting bits here happen in Javascript, but one of
           our files sees changes
      •    address/templates/address/contact.html is refactored to
           instantiate our Backbone objects when the page loads
      •    address/static/js/address.js is where the magic happens
      •    The app doesn’t have much going on yet, but it does work!




18   w w w . c o m p e t e . c o m
Add-delete, a deeper look at Backbone

      •    Backbone models support validation, which we add to our
           ContactModel
             •   Validation on ContactModel
             •   Error handler on the view
             •   Error events versus error callback

      •    Backbone views do easy event delegation. We put an event
           on our ContactModelView to catch the click on the delete
           button
             •   Entry in events hash
             •   Click handler
             •   Destroy handler




19   w w w . c o m p e t e . c o m
Add-edit, making all those pieces dance

      •    Add an extra “edit row” to our template
             •   Hide by default, and toggle whether edit or display is activated

      •    Handle submit of edit row
             •   Process edit and save changes to the model
             •   Close the edit after submit
             •   Handle validation errors




20   w w w . c o m p e t e . c o m
Epilogue: Extending the app




21   w w w . c o m p e t e . c o m
Group up and pick a project or two!

      •    Add validation – require fields, require capitalization?
      •    Add additional fields
      •    Enforce uniqueness on email address
      •    Enforce Django validation (and propagate errors properly)
      •    Refactor the Underscore templates to live in the DOM
      •    Or for the truly ambitious
             •   Add tracking of communications with a contact
             •   Permit multiple sets of contact information per name
             •   Add a “detailed view” panel for additional attributes

      •    Or a personally motivated project


22   w w w . c o m p e t e . c o m

Mais conteúdo relacionado

Mais procurados

Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3Wen-Tien Chang
 
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...kiphampton
 
2011 NetUG HH: ASP.NET MVC & HTML 5
2011 NetUG HH: ASP.NET MVC & HTML 52011 NetUG HH: ASP.NET MVC & HTML 5
2011 NetUG HH: ASP.NET MVC & HTML 5Daniel Fisher
 
Ruby on Rails Security
Ruby on Rails SecurityRuby on Rails Security
Ruby on Rails Securityamiable_indian
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Coursepeter_marklund
 
When Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźWhen Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźAEM HUB
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsAgnieszka Figiel
 
Building search app with ElasticSearch
Building search app with ElasticSearchBuilding search app with ElasticSearch
Building search app with ElasticSearchLukas Vlcek
 
Making Life Easier with PowerShell - SPSRIC
Making Life Easier with PowerShell - SPSRICMaking Life Easier with PowerShell - SPSRIC
Making Life Easier with PowerShell - SPSRICMichael Greene
 
Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!Doris Chen
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon RailsPaul Pajo
 
An Introduction to Ruby on Rails
An Introduction to Ruby on RailsAn Introduction to Ruby on Rails
An Introduction to Ruby on RailsJoe Fiorini
 
MVC Frameworks for building PHP Web Applications
MVC Frameworks for building PHP Web ApplicationsMVC Frameworks for building PHP Web Applications
MVC Frameworks for building PHP Web ApplicationsVforce Infotech
 
Writing great documentation - CodeConf 2011
Writing great documentation - CodeConf 2011Writing great documentation - CodeConf 2011
Writing great documentation - CodeConf 2011Jacob Kaplan-Moss
 
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...Matt Raible
 

Mais procurados (20)

Fast mobile web apps
Fast mobile web appsFast mobile web apps
Fast mobile web apps
 
Learning Rails
Learning RailsLearning Rails
Learning Rails
 
Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3
 
Apache Wicket
Apache WicketApache Wicket
Apache Wicket
 
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...
 
2011 NetUG HH: ASP.NET MVC & HTML 5
2011 NetUG HH: ASP.NET MVC & HTML 52011 NetUG HH: ASP.NET MVC & HTML 5
2011 NetUG HH: ASP.NET MVC & HTML 5
 
Ruby on Rails Security
Ruby on Rails SecurityRuby on Rails Security
Ruby on Rails Security
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
 
Wt unit 1 ppts web development process
Wt unit 1 ppts web development processWt unit 1 ppts web development process
Wt unit 1 ppts web development process
 
Angular js
Angular jsAngular js
Angular js
 
When Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźWhen Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz Niedźwiedź
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Building search app with ElasticSearch
Building search app with ElasticSearchBuilding search app with ElasticSearch
Building search app with ElasticSearch
 
Making Life Easier with PowerShell - SPSRIC
Making Life Easier with PowerShell - SPSRICMaking Life Easier with PowerShell - SPSRIC
Making Life Easier with PowerShell - SPSRIC
 
Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
 
An Introduction to Ruby on Rails
An Introduction to Ruby on RailsAn Introduction to Ruby on Rails
An Introduction to Ruby on Rails
 
MVC Frameworks for building PHP Web Applications
MVC Frameworks for building PHP Web ApplicationsMVC Frameworks for building PHP Web Applications
MVC Frameworks for building PHP Web Applications
 
Writing great documentation - CodeConf 2011
Writing great documentation - CodeConf 2011Writing great documentation - CodeConf 2011
Writing great documentation - CodeConf 2011
 
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
 

Semelhante a Build Quick AJAX Apps with Django and Backbone.js

Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Dimitri de Putte
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners WorkshopSathish VJ
 
AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)Igor Talevski
 
ZZ BC#7 asp.net mvc practice and guideline by NineMvp
ZZ BC#7 asp.net mvc practice and guideline by NineMvpZZ BC#7 asp.net mvc practice and guideline by NineMvp
ZZ BC#7 asp.net mvc practice and guideline by NineMvpChalermpon Areepong
 
Integrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMSIntegrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMSTom Borger
 
E-Commerce Applications Development
E-Commerce Applications Development E-Commerce Applications Development
E-Commerce Applications Development Muhammad Sajid
 
End to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSEnd to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSGil Fink
 
Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1Henry S
 
Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts weili_at_slideshare
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applicationsIvano Malavolta
 
Apache Cayenne for WO Devs
Apache Cayenne for WO DevsApache Cayenne for WO Devs
Apache Cayenne for WO DevsWO Community
 

Semelhante a Build Quick AJAX Apps with Django and Backbone.js (20)

Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012
 
AngularJS
AngularJSAngularJS
AngularJS
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 
AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
ZZ BC#7 asp.net mvc practice and guideline by NineMvp
ZZ BC#7 asp.net mvc practice and guideline by NineMvpZZ BC#7 asp.net mvc practice and guideline by NineMvp
ZZ BC#7 asp.net mvc practice and guideline by NineMvp
 
Asp 1a-aspnetmvc
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvc
 
Aspnetmvc 1
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
 
[2015/2016] Backbone JS
[2015/2016] Backbone JS[2015/2016] Backbone JS
[2015/2016] Backbone JS
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Angular js 1.0-fundamentals
Angular js 1.0-fundamentalsAngular js 1.0-fundamentals
Angular js 1.0-fundamentals
 
Integrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMSIntegrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMS
 
E-Commerce Applications Development
E-Commerce Applications Development E-Commerce Applications Development
E-Commerce Applications Development
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
WebDev Crash Course
WebDev Crash CourseWebDev Crash Course
WebDev Crash Course
 
End to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSEnd to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJS
 
Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1
 
Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applications
 
Apache Cayenne for WO Devs
Apache Cayenne for WO DevsApache Cayenne for WO Devs
Apache Cayenne for WO Devs
 

Último

#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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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
 
[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
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
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
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
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
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
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
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Último (20)

#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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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
 
[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
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
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
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
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
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Build Quick AJAX Apps with Django and Backbone.js

  • 1. Show Some Spine! quick AJAX apps with Django&Backbone.js w w w . c o m p e t e . c o m
  • 2. An Overview of Our Evening’s Entertainment • Ridiculously fast introduction to Backbone.js • Building our AJAX app, a comedy in five acts • Foundation • The Django bits • AJAX functionality • Adding a delete feature and some validation • Adding in editing • Workshop: extend the app, ask questions, try something new 2 w w w . c o m p e t e . c o m
  • 3. Backbone.js at 120mph 3 w w w . c o m p e t e . c o m
  • 4. A First Date with Backbone.js • AJAX apps turn into spaghetti pretty fast • Javascript OOP is the Wild West of object systems • Both of which spell doom for the tidy, reusable widgets imagined by your clever UI design • So you might be thinking of borrowing someone else’s widget library • But Backbone.js … • … offers an MVC-ish framework for your front end • … provides a basically sane object system • … is small, reasonably easy to read, and has annotated source • … works with a simple REST API to the backend server 4 w w w . c o m p e t e . c o m
  • 5. A Discreet Aside on Architecture: MVC • MVC, aka Model/View/Controller, aka Model/View promotes clarity of design • Good design is the art of designing simple, flexible interfaces • Components have well-defined responsibilities • Components are intertwined only at interface boundaries • MVC fits most applications, and supports good design • The “Model” stores data and is responsible for mutating that data • The “View” is a display of the Model’s data • The “Controller” manages the interactions between Model and View 5 w w w . c o m p e t e . c o m
  • 6. A Discreet Aside on Architecture: REST • REST is short for “representational state transfer” • REST refers to the use of the HTTP protocol features to construct an API • For instance: • An HTTP GET might fetch a data object from the server • An HTTP POST could insert a new data object • PUT can be used to update, and DELETE would naturally delete • HTTP response codes can be used to indicate status of the request • REST provides a very simple interface for exchanging data • Contrast with SOAP or XML-RPC 6 w w w . c o m p e t e . c o m
  • 7. Backbone’s Kinda MVC • Models and Collections manage synchronization with the server layer • Models represent a RESTful object • Collections are a tidy way of managing lists of Models • Views tie into the DOM and provide event handling semantics • Routers can be used to map URLs or URL fragments onto functionality • Templates can be lifted from the DOM, or prepared in the Javascript 7 w w w . c o m p e t e . c o m
  • 8. Backbone’s Models • Models use a REST API to sync with the server • Model or Collection is provided with a URL • Backbone uses URL + id to perform insert, delete, update, and fetch • Models store their state in an “attributes” object • Models raise a variety of events: • change whenever the attributes change • destroy when the model is destroyed • error when a validation fails • Additional methods and events can be added to the basic Model class to create a full-featured model 8 w w w . c o m p e t e . c o m
  • 9. Backbone’s Collections • Collections are a grouping of Models • Collections can be arbitrary groups, or the complete set of available objects belonging to a particular Model subclass • Collections raise a variety of events: • reset whenever the whole collection is fetched or set • will re-raise events from Models in the collection • add and remove will fire when Models are added or removed • Collections can define a comparator to enforce a sort order • A comparator is a function that transforms a model into a sortable primitive • The sort method itself can also be overridden 9 w w w . c o m p e t e . c o m
  • 10. Backbone’s Views • Views attach to a DOM element, which can be passed in, or can be created by the view • Views provide an events object which maps event selectors to event handlers • Views provide convenience features for working with JQuery to manipulate the DOM • Views typically couple with a Collection, a Model, or both • Views typically use a template or a DOM fragment to dynamically render themselves 10 w w w . c o m p e t e . c o m
  • 11. Underscore templates & DOM fragments • My basic setup customizes Underscore’s templates to have Django-like syntax • {{ name }} interpolates the context variable name • {% javascript = “spammy”; %} interpolates arbitrary Javascript into the template – mostly useful for conditionals and loops and such • Underscore templates are just Javascript strings • Underscore compiles them into functions • The functions accept a context object as an argument • An alternate approach uses chunks of HTML hidden on the page and copied out with JQuery 11 w w w . c o m p e t e . c o m
  • 12. Putting it all together 12 w w w . c o m p e t e . c o m
  • 13. A Sample Backbone + Django App 13 w w w . c o m p e t e . c o m
  • 14. Address: the app • On Github: https://github.com/ggerrietts/vertebrate-django • Built around a simple contact record: first, last, email • Displays all contacts in the database • Allows entry of a new contact • Validates entered email addresses against a simple regex • Enables deletion of contacts from the database • Permits (inline) editing of the records in the database 14 w w w . c o m p e t e . c o m
  • 15. App layout • Common provides /common framework elements /common/static/{js,css,ext} • Third-party components /common/templates /common/views.py • Master template /common/models.py • “Library” classes /address • Address is our specific /address/static/{js,css,img} application /address/templates • App-specific media /address/views.py /address/models.py • App template(s) /address/admin.py • Actual models & views /urls.py /settings.py 15 w w w . c o m p e t e . c o m
  • 16. The app skeleton • The app skeleton provides some frameworky infrastructure • common/views.py maps a REST API onto Django models • common/models.py has the default serialization mechanics • common/static/ext has our third-party dependencies • common/static/js has some customization code • The skeleton also has a master template and master CSS file 16 w w w . c o m p e t e . c o m
  • 17. Django-emails, the Django app • address/models.py defines our contact record • address/views.py is a simple REST-powered view • address/templates/address/contact.html lays a foundation to build our app on • This branch also adds a CSS file and some images • We add an admin.py file to power up the admin interface • And we builds out the global urls.py 17 w w w . c o m p e t e . c o m
  • 18. Backbone-emails, a functional app • All the interesting bits here happen in Javascript, but one of our files sees changes • address/templates/address/contact.html is refactored to instantiate our Backbone objects when the page loads • address/static/js/address.js is where the magic happens • The app doesn’t have much going on yet, but it does work! 18 w w w . c o m p e t e . c o m
  • 19. Add-delete, a deeper look at Backbone • Backbone models support validation, which we add to our ContactModel • Validation on ContactModel • Error handler on the view • Error events versus error callback • Backbone views do easy event delegation. We put an event on our ContactModelView to catch the click on the delete button • Entry in events hash • Click handler • Destroy handler 19 w w w . c o m p e t e . c o m
  • 20. Add-edit, making all those pieces dance • Add an extra “edit row” to our template • Hide by default, and toggle whether edit or display is activated • Handle submit of edit row • Process edit and save changes to the model • Close the edit after submit • Handle validation errors 20 w w w . c o m p e t e . c o m
  • 21. Epilogue: Extending the app 21 w w w . c o m p e t e . c o m
  • 22. Group up and pick a project or two! • Add validation – require fields, require capitalization? • Add additional fields • Enforce uniqueness on email address • Enforce Django validation (and propagate errors properly) • Refactor the Underscore templates to live in the DOM • Or for the truly ambitious • Add tracking of communications with a contact • Permit multiple sets of contact information per name • Add a “detailed view” panel for additional attributes • Or a personally motivated project 22 w w w . c o m p e t e . c o m

Notas do Editor

  1. Weird, right? Because I’m pretty sure Ajax was a Greek?Maybe Lua is the Wild West. Maybe Javascript is more like the Somali coastline
  2. Of course, knowing what REST stands for tells you very little about what it means
  3. I tend to think of the “views” as the controllers, here, but I think Routers could qualify as well. We just don’t often use them.Django calls itself “model / view / template” and I think that’s sort of how we at compete have been working yuhhuu8with Backbone, too.
  4. Hey, I’m a DIV! I’m going to take data from my Model, plug it into this template, and that will be my HTML! If my Model changes, I will redraw myself!
  5. Here solid lines represent “ownership” or direct access. A view has a “model” attribute that it can use to get data from its model. A dotted line represents an event channel. A model raises events to notify any Views that might be interested in its state. In the collection diagram, we can see that the model raises events back to its collection as well.
  6. I’ve been told several times in my career that the only real reason to have a website is so you can collect email addresses to send marketing to. So, that’s what we’re going to build, an application that collects email addresses.
  7. Included a “class” that creates a generic OO syntax