WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
- 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
- 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><script></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><script></strong>"
- 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
- 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
- 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' });
- 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
- 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
Editor's Notes
- If it is restful, then saving models in this collection will automatically PUT to /posts/{id}