SlideShare a Scribd company logo
Introduction to Backbone.js in WordPress
Brian Hogg
brianhogg.com | @brianhogg
WordCamp Ann Arbor 2015
brianhogg.com
Subtitle
Section Divider Title Layout
Subtitle
Event Calendar Newsletter
https://wordpress.org/plugins/event-calendar-newsletter/
Agenda
▪ Why Backbone.js
▪ Basics of Backbone.js / Underscore.js
▪ Example plugin using wp.template, wp.Backbone.View and WP JSON REST
API v2
Who are you?
Why Backbone.js vs just jQuery?
▪ Events system
▪ Performance
▪ Leveraging the community
▪ Not re-inventing the wheel
▪ Enforces some structure on your JavaScript code
Backbone.js has an MV* structure
Uses jQuery but only hard requirement is Underscore.js
What is Underscore.js?
Utility functions with _
_.each, _.template, Lots more: http://documentcloud.github.io/underscore/
Templates
var template = _.template("hello <%= name %>");
var html = template({ name: 'Brian' });
console.log( html ); // "hello Brian"
var template = _.template("<strong><%- value %></strong>");
var html = template({ value: '<script>' });
console.log( html ); // "<strong>&lt;script&gt;</strong>"
Templates
var template = _.template("hello <%= name %>");
var html = template({ name: 'Brian' });
console.log( html ); // "hello Brian”
var template = _.template("<strong><%- value %></strong>");
var html = template({ value: '<script>' });
console.log( html ); // "<strong>&lt;script&gt;</strong>"
wp.template
wp.template
hello {{ name }}
<strong>{{{ value }}}</strong>
hello <%= name %>
<strong><%- value %></strong>
wp.template
<# if ( some_condition ) { #>
some output
<# } #>
Lots of Alternatives
Ember.JS, Angular.JS, ...
Multiple ways of doing similar things. Even in Backbone.JS:
“It's common for folks just getting started to treat the examples listed on
this page as some sort of gospel truth. In fact, Backbone.js is intended to
be fairly agnostic about many common patterns in client-side code.”
http://backbonejs.org/#FAQ-tim-toady
Backbone / Underscore
Included in WordPress since 3.5
The "backbone" of the media manager, revisions UI
Models
“Models are the heart of any JavaScript application,
containing the interactive data as well as a large part
of the logic surrounding it: conversions, validations,
computed properties, and access control. You extend
Backbone.Model with your domain-specific methods,
and Model provides a basic set of functionality for
managing changes.”
Models Example
var Post = Backbone.Model.extend({
defaults: {
title: "",
post_status: "draft"
},
initialize: function() {
console.log("creating a post");
}
});
var post = new Post({ title: "Hello, world", post_status: "draft" });
var title = post.get("title"); // Hello, world
var post_status = post.get("post_status"); // draft
Listening for Changes
post.on("change:title", function(model) {
alert("Title changed to: " + model.get("title"));
});
this.on("change:title", this.titleChanged)
or in the initialize function of a model with:
Views
▪ Used to turn a model into something you can see
▪ Always contains a DOM element (the el property) though it may not have been
added to the viewable page yet
Bare Minimum to use Backbone
Group your events code together
var PostView = Backbone.View.extend({
events: {
"click .edit": "editPost",
"change .post_status": "statusChanged"
},
editPost: function(event) {
// ...
},
statusChanged: function(event) {
// ...
}
});
var postView = new PostView({ el: '#my-form' });
wp.template
<script id="tmpl-bb-post" type="text/html">
{{{ data.title }}}
</script>
bbdemo.PostView = wp.Backbone.View.extend({
template: wp.template(‘bb-post'),
prepare: function() {
return this.model.toJSON();
}
});
Collections
Ordered set of models
var Posts = Backbone.Collection.extend({
model: Post
});
var post1 = new Post({ title: "Hello, world" });
var post2 = new Post({ title: "Sample page" });
var myPosts = new Posts([ post1, post2 ]);
Populating Collection from Server
WP REST API[
{
id: 1,
title: {
rendered: "Hello, World"
}
},
{
id: 3,
title: {
rendered: "Ann Arbor is amazing"
}
},
]
</script>
Populating Collections from Server
var Posts = Backbone.Collection.extend({
model: Post,
url: bbdemo.url + '/posts'
});
var postsCollection = new Posts();
postsCollection.fetch();
If not a RESTful format, would need to override functions like fetch, sync and parse
How Backbone Works With REST APIs
Out of the box, Backbone.js supports RESTful APIs through
Backbone.sync():
create → POST /collection
read → GET /collection[/id]
update → PUT /collection/id
patch → PATCH /collection/id
delete → DELETE /collection/id
DEMO
Resources
WP API JavaScript Client
https://github.com/WP-API/client-js
Backbone Debugger for Chrome
https://chrome.google.com/webstore/detail/backbone-debugger/bhljhndlimiafopmmhjlg
Example wp.Backbone.View plugin
https://github.com/markjaquith/Showdown
Code / Slides
https://github.com/brianhogg
http://www.slideshare.net/bhogg
Introduction to Backbone.js in WordPress
Brian Hogg
brianhogg.com | @brianhogg
WordCamp Ann Arbor 2015

More Related Content

What's hot

Webpack DevTalk
Webpack DevTalkWebpack DevTalk
Webpack DevTalk
Alessandro Bellini
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecture
postrational
 
Webpack
WebpackWebpack
Webpack
Anjali Chawla
 
Build Your Own CMS with Apache Sling
Build Your Own CMS with Apache SlingBuild Your Own CMS with Apache Sling
Build Your Own CMS with Apache Sling
Bob Paulin
 
Packing it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to nowPacking it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to now
Derek Willian Stavis
 
An Intro into webpack
An Intro into webpackAn Intro into webpack
An Intro into webpack
Squash Apps Pvt Ltd
 
Webpack: from 0 to 2
Webpack: from 0 to 2Webpack: from 0 to 2
Webpack: from 0 to 2
Alessandro Bellini
 
Grunt.js and Yeoman, Continous Integration
Grunt.js and Yeoman, Continous IntegrationGrunt.js and Yeoman, Continous Integration
Grunt.js and Yeoman, Continous Integration
David Amend
 
遠端團隊專案建立與管理 remote team management 2016
遠端團隊專案建立與管理 remote team management 2016遠端團隊專案建立與管理 remote team management 2016
遠端團隊專案建立與管理 remote team management 2016
Caesar Chi
 
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Acquia
 
Webinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST APIWebinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST API
WP Engine UK
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript Framework
All Things Open
 
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi   SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
Sencha
 
Build RESTful API Using Express JS
Build RESTful API Using Express JSBuild RESTful API Using Express JS
Build RESTful API Using Express JS
Cakra Danu Sedayu
 
Cloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and GebCloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and Geb
David Carr
 
Here Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPressHere Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPress
Rami Sayar
 
Sails.js Intro
Sails.js IntroSails.js Intro
Sails.js Intro
Nicholas Jansma
 
Express JS
Express JSExpress JS
Express JS
Designveloper
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
Edureka!
 
Single Page Apps with Drupal 8
Single Page Apps with Drupal 8Single Page Apps with Drupal 8
Single Page Apps with Drupal 8
Chris Tankersley
 

What's hot (20)

Webpack DevTalk
Webpack DevTalkWebpack DevTalk
Webpack DevTalk
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecture
 
Webpack
WebpackWebpack
Webpack
 
Build Your Own CMS with Apache Sling
Build Your Own CMS with Apache SlingBuild Your Own CMS with Apache Sling
Build Your Own CMS with Apache Sling
 
Packing it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to nowPacking it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to now
 
An Intro into webpack
An Intro into webpackAn Intro into webpack
An Intro into webpack
 
Webpack: from 0 to 2
Webpack: from 0 to 2Webpack: from 0 to 2
Webpack: from 0 to 2
 
Grunt.js and Yeoman, Continous Integration
Grunt.js and Yeoman, Continous IntegrationGrunt.js and Yeoman, Continous Integration
Grunt.js and Yeoman, Continous Integration
 
遠端團隊專案建立與管理 remote team management 2016
遠端團隊專案建立與管理 remote team management 2016遠端團隊專案建立與管理 remote team management 2016
遠端團隊專案建立與管理 remote team management 2016
 
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
 
Webinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST APIWebinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST API
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript Framework
 
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi   SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
 
Build RESTful API Using Express JS
Build RESTful API Using Express JSBuild RESTful API Using Express JS
Build RESTful API Using Express JS
 
Cloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and GebCloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and Geb
 
Here Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPressHere Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPress
 
Sails.js Intro
Sails.js IntroSails.js Intro
Sails.js Intro
 
Express JS
Express JSExpress JS
Express JS
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
 
Single Page Apps with Drupal 8
Single Page Apps with Drupal 8Single Page Apps with Drupal 8
Single Page Apps with Drupal 8
 

Similar to WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API

Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentation
Brian Hogg
 
Using RequireJS with CakePHP
Using RequireJS with CakePHPUsing RequireJS with CakePHP
Using RequireJS with CakePHP
Stephen Young
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable Applications
Alessandro Molina
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!
Eric Palakovich Carr
 
Javascript ui for rest services
Javascript ui for rest servicesJavascript ui for rest services
Javascript ui for rest services
Ioan Eugen Stan
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
toddbr
 
Backbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVCBackbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVC
pootsbook
 
Javascript
JavascriptJavascript
Javascript
Adil Jafri
 
Javascript frameworks: Backbone.js
Javascript frameworks: Backbone.jsJavascript frameworks: Backbone.js
Javascript frameworks: Backbone.js
Soós Gábor
 
Modular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJSModular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJS
Gunnar Hillert
 
27javascript
27javascript27javascript
27javascript
Adil Jafri
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
Bert Wijnants
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
Knoldus Inc.
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
Mike Subelsky
 
Backbone js
Backbone jsBackbone js
Backbone js
Knoldus Inc.
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
Sérgio Santos
 
Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!
Doris Chen
 
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
mfrancis
 
OSGi and Spring Data for simple (Web) Application Development
OSGi and Spring Data  for simple (Web) Application DevelopmentOSGi and Spring Data  for simple (Web) Application Development
OSGi and Spring Data for simple (Web) Application Development
Christian Baranowski
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
Nick Lee
 

Similar to WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API (20)

Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentation
 
Using RequireJS with CakePHP
Using RequireJS with CakePHPUsing RequireJS with CakePHP
Using RequireJS with CakePHP
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable Applications
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!
 
Javascript ui for rest services
Javascript ui for rest servicesJavascript ui for rest services
Javascript ui for rest services
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
Backbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVCBackbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVC
 
Javascript
JavascriptJavascript
Javascript
 
Javascript frameworks: Backbone.js
Javascript frameworks: Backbone.jsJavascript frameworks: Backbone.js
Javascript frameworks: Backbone.js
 
Modular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJSModular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJS
 
27javascript
27javascript27javascript
27javascript
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!
 
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
 
OSGi and Spring Data for simple (Web) Application Development
OSGi and Spring Data  for simple (Web) Application DevelopmentOSGi and Spring Data  for simple (Web) Application Development
OSGi and Spring Data for simple (Web) Application Development
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
 

More from Brian Hogg

Submitting, maintaining and growing a plugin on wp.org
Submitting, maintaining and growing a plugin on wp.orgSubmitting, maintaining and growing a plugin on wp.org
Submitting, maintaining and growing a plugin on wp.org
Brian Hogg
 
Preparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for TranslationPreparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for Translation
Brian Hogg
 
Your WordPress Freelance Business Site
Your WordPress Freelance Business SiteYour WordPress Freelance Business Site
Your WordPress Freelance Business Site
Brian Hogg
 
Things I've Learned About Creating a Premium Plugin
Things I've Learned About Creating a Premium PluginThings I've Learned About Creating a Premium Plugin
Things I've Learned About Creating a Premium Plugin
Brian Hogg
 
WordPress 4.4 and Upgrading Your Site (without the tears...)
WordPress 4.4 and Upgrading Your Site (without the tears...)WordPress 4.4 and Upgrading Your Site (without the tears...)
WordPress 4.4 and Upgrading Your Site (without the tears...)
Brian Hogg
 
Using Actions and Filters in WordPress to Make a Plugin Your Own
Using Actions and Filters in WordPress to Make a Plugin Your OwnUsing Actions and Filters in WordPress to Make a Plugin Your Own
Using Actions and Filters in WordPress to Make a Plugin Your Own
Brian Hogg
 
Things I learned about Stress and Imposter Syndrome in Tech
Things I learned about Stress and Imposter Syndrome in TechThings I learned about Stress and Imposter Syndrome in Tech
Things I learned about Stress and Imposter Syndrome in Tech
Brian Hogg
 
Pricing and Marketing for Freelancers - How to?
Pricing and Marketing for Freelancers - How to?Pricing and Marketing for Freelancers - How to?
Pricing and Marketing for Freelancers - How to?
Brian Hogg
 
Introduction to test driven development
Introduction to test driven developmentIntroduction to test driven development
Introduction to test driven development
Brian Hogg
 
Working with Geolocation in Wordpress
Working with Geolocation in WordpressWorking with Geolocation in Wordpress
Working with Geolocation in Wordpress
Brian Hogg
 
Intro to Web Apps using HTML5 and Javascript
Intro to Web Apps using HTML5 and JavascriptIntro to Web Apps using HTML5 and Javascript
Intro to Web Apps using HTML5 and Javascript
Brian Hogg
 

More from Brian Hogg (11)

Submitting, maintaining and growing a plugin on wp.org
Submitting, maintaining and growing a plugin on wp.orgSubmitting, maintaining and growing a plugin on wp.org
Submitting, maintaining and growing a plugin on wp.org
 
Preparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for TranslationPreparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for Translation
 
Your WordPress Freelance Business Site
Your WordPress Freelance Business SiteYour WordPress Freelance Business Site
Your WordPress Freelance Business Site
 
Things I've Learned About Creating a Premium Plugin
Things I've Learned About Creating a Premium PluginThings I've Learned About Creating a Premium Plugin
Things I've Learned About Creating a Premium Plugin
 
WordPress 4.4 and Upgrading Your Site (without the tears...)
WordPress 4.4 and Upgrading Your Site (without the tears...)WordPress 4.4 and Upgrading Your Site (without the tears...)
WordPress 4.4 and Upgrading Your Site (without the tears...)
 
Using Actions and Filters in WordPress to Make a Plugin Your Own
Using Actions and Filters in WordPress to Make a Plugin Your OwnUsing Actions and Filters in WordPress to Make a Plugin Your Own
Using Actions and Filters in WordPress to Make a Plugin Your Own
 
Things I learned about Stress and Imposter Syndrome in Tech
Things I learned about Stress and Imposter Syndrome in TechThings I learned about Stress and Imposter Syndrome in Tech
Things I learned about Stress and Imposter Syndrome in Tech
 
Pricing and Marketing for Freelancers - How to?
Pricing and Marketing for Freelancers - How to?Pricing and Marketing for Freelancers - How to?
Pricing and Marketing for Freelancers - How to?
 
Introduction to test driven development
Introduction to test driven developmentIntroduction to test driven development
Introduction to test driven development
 
Working with Geolocation in Wordpress
Working with Geolocation in WordpressWorking with Geolocation in Wordpress
Working with Geolocation in Wordpress
 
Intro to Web Apps using HTML5 and Javascript
Intro to Web Apps using HTML5 and JavascriptIntro to Web Apps using HTML5 and Javascript
Intro to Web Apps using HTML5 and Javascript
 

Recently uploaded

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 

Recently uploaded (20)

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 

WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API

  • 1. Introduction to Backbone.js in WordPress Brian Hogg brianhogg.com | @brianhogg WordCamp Ann Arbor 2015
  • 3. Section Divider Title Layout Subtitle
  • 5. Agenda ▪ Why Backbone.js ▪ Basics of Backbone.js / Underscore.js ▪ Example plugin using wp.template, wp.Backbone.View and WP JSON REST API v2
  • 7. Why Backbone.js vs just jQuery? ▪ Events system ▪ Performance ▪ Leveraging the community ▪ Not re-inventing the wheel ▪ Enforces some structure on your JavaScript code
  • 8. Backbone.js has an MV* structure Uses jQuery but only hard requirement is Underscore.js
  • 9. What is Underscore.js? Utility functions with _ _.each, _.template, Lots more: http://documentcloud.github.io/underscore/
  • 10. Templates var template = _.template("hello <%= name %>"); var html = template({ name: 'Brian' }); console.log( html ); // "hello Brian" var template = _.template("<strong><%- value %></strong>"); var html = template({ value: '<script>' }); console.log( html ); // "<strong>&lt;script&gt;</strong>"
  • 11. Templates var template = _.template("hello <%= name %>"); var html = template({ name: 'Brian' }); console.log( html ); // "hello Brian” var template = _.template("<strong><%- value %></strong>"); var html = template({ value: '<script>' }); console.log( html ); // "<strong>&lt;script&gt;</strong>"
  • 13. wp.template hello {{ name }} <strong>{{{ value }}}</strong> hello <%= name %> <strong><%- value %></strong>
  • 14. wp.template <# if ( some_condition ) { #> some output <# } #>
  • 15. Lots of Alternatives Ember.JS, Angular.JS, ... Multiple ways of doing similar things. Even in Backbone.JS: “It's common for folks just getting started to treat the examples listed on this page as some sort of gospel truth. In fact, Backbone.js is intended to be fairly agnostic about many common patterns in client-side code.” http://backbonejs.org/#FAQ-tim-toady
  • 16. Backbone / Underscore Included in WordPress since 3.5 The "backbone" of the media manager, revisions UI
  • 17. Models “Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control. You extend Backbone.Model with your domain-specific methods, and Model provides a basic set of functionality for managing changes.”
  • 18. Models Example var Post = Backbone.Model.extend({ defaults: { title: "", post_status: "draft" }, initialize: function() { console.log("creating a post"); } }); var post = new Post({ title: "Hello, world", post_status: "draft" }); var title = post.get("title"); // Hello, world var post_status = post.get("post_status"); // draft
  • 19. Listening for Changes post.on("change:title", function(model) { alert("Title changed to: " + model.get("title")); }); this.on("change:title", this.titleChanged) or in the initialize function of a model with:
  • 20. Views ▪ Used to turn a model into something you can see ▪ Always contains a DOM element (the el property) though it may not have been added to the viewable page yet
  • 21. Bare Minimum to use Backbone Group your events code together var PostView = Backbone.View.extend({ events: { "click .edit": "editPost", "change .post_status": "statusChanged" }, editPost: function(event) { // ... }, statusChanged: function(event) { // ... } }); var postView = new PostView({ el: '#my-form' });
  • 22. wp.template <script id="tmpl-bb-post" type="text/html"> {{{ data.title }}} </script> bbdemo.PostView = wp.Backbone.View.extend({ template: wp.template(‘bb-post'), prepare: function() { return this.model.toJSON(); } });
  • 23. Collections Ordered set of models var Posts = Backbone.Collection.extend({ model: Post }); var post1 = new Post({ title: "Hello, world" }); var post2 = new Post({ title: "Sample page" }); var myPosts = new Posts([ post1, post2 ]);
  • 24. Populating Collection from Server WP REST API[ { id: 1, title: { rendered: "Hello, World" } }, { id: 3, title: { rendered: "Ann Arbor is amazing" } }, ] </script>
  • 25. Populating Collections from Server var Posts = Backbone.Collection.extend({ model: Post, url: bbdemo.url + '/posts' }); var postsCollection = new Posts(); postsCollection.fetch(); If not a RESTful format, would need to override functions like fetch, sync and parse
  • 26. How Backbone Works With REST APIs Out of the box, Backbone.js supports RESTful APIs through Backbone.sync(): create → POST /collection read → GET /collection[/id] update → PUT /collection/id patch → PATCH /collection/id delete → DELETE /collection/id
  • 27.
  • 28. DEMO
  • 29. Resources WP API JavaScript Client https://github.com/WP-API/client-js Backbone Debugger for Chrome https://chrome.google.com/webstore/detail/backbone-debugger/bhljhndlimiafopmmhjlg Example wp.Backbone.View plugin https://github.com/markjaquith/Showdown Code / Slides https://github.com/brianhogg http://www.slideshare.net/bhogg
  • 30. Introduction to Backbone.js in WordPress Brian Hogg brianhogg.com | @brianhogg WordCamp Ann Arbor 2015

Editor's Notes

  1. If it is restful, then saving models in this collection will automatically PUT to /posts/{id}