SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Fast mobile web apps
Fast mobile web appsFast mobile web apps
Fast mobile web appsIvano Malavolta
 
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
 

Was ist angesagt? (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...
 

Ă„hnlich wie 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
AngularJSAngularJS
AngularJSYogesh L
 
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
 
Asp 1a-aspnetmvc
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvcFajar Baskoro
 
[2015/2016] Backbone JS
[2015/2016] Backbone JS[2015/2016] Backbone JS
[2015/2016] Backbone JSIvano Malavolta
 
Angular js 1.0-fundamentals
Angular js 1.0-fundamentalsAngular js 1.0-fundamentals
Angular js 1.0-fundamentalsVenkatesh Narayanan
 
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
 
WebDev Crash Course
WebDev Crash CourseWebDev Crash Course
WebDev Crash CourseCesar Martinez
 
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
 

Ă„hnlich wie 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
 

KĂĽrzlich hochgeladen

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
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 Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
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
 
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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
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
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
#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
 

KĂĽrzlich hochgeladen (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
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 Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
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
 
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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 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
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 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

Hinweis der Redaktion

  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