18
Backbone John Ashmead 1 Wednesday, May 2, 2012

Overview of Backbone

Embed Size (px)

DESCRIPTION

What Backbone.js is, how to use it in building websites, MVC architecture, RESTful abstractions, …

Citation preview

Page 1: Overview of Backbone

BackboneJohn Ashmead

1Wednesday, May 2, 2012

Page 2: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

MVC Abstraction Layer

• Events

• Models

• Views

• Collections

• Routers

2Wednesday, May 2, 2012

Page 3: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Events

• Used for backbone’s events

• On, off, trigger

• You can role your own, e.g. “selected:true”

• Simple linked list

• Per instance

• Mixed into Models, Views, Collections, & Routers

3Wednesday, May 2, 2012

Page 4: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Event callsobject.on(event, callback, [context]);// aka bind

object.off([event], [callback], [context]);

// aka unbindobject.trigger(event, [*args]);

// event = name[:namespace]

4Wednesday, May 2, 2012

Page 5: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Models

• A unique ‘cId’ supplied, you set ‘id’

• “change” events issued when you use get, set to change (you can bypass)

• toJSON, fetch, parse, clone, previous, previousAttributes, save, destroy, validate, …

5Wednesday, May 2, 2012

Page 6: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Model callsvar MasterPiece = Backbone.Model.extend({ option1: value1, initialize: function(arg1, …) {}});

masterPiece1 = new MasterPiece();

masterPiece1.get('option1');

masterPiece1.set('option1', 'value1');

6Wednesday, May 2, 2012

Page 7: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

RESTful interface• “Backbone.sync” is basic call

• Sends server a “POST”, “GET”, “PUT”, or “DELETE”, depending on whether you have requested a create, retrieve, update, or delete.

• If you define MasterPiece.url(), sync will talk to url’s of the form it returns

• Example: /masterpieces/1

• “isNew” tracks status on client

7Wednesday, May 2, 2012

Page 8: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Views

• Always gets a default ‘el’ DOM element

• Has view.$el defined as $(view.el) as well

• Use “setElement” to change its element

• Instantiate “render” method to get it to work

8Wednesday, May 2, 2012

Page 9: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

View codevar Picture = Backbone.View.extend({ render: function(eventName) { this.$el.html(this.template( this.model.toJSON())); return this; }, events: {“change”: “render”}, template: _.template(“<div><%= title =%></div>”), close: { this.$el.unbind(); this.$el.empty();});var picture1 = new Picture({ model: monaLisa1 });

9Wednesday, May 2, 2012

Page 10: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Collections

• Ordered sets of models

• Can be the “model” in a view

• Can be included in a model

• Usual functions, including most of Underscore

• Models have a “collection” property, so only one collection per model

10Wednesday, May 2, 2012

Page 11: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Underscore

• Each, map, find, filter, reject,…

• First, last, flatten, uniq, …

• Memoize, delay, throttle, …

• Keys, values, extend, clone, defaults, …

11Wednesday, May 2, 2012

Page 12: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Routers

• Keeps a map of hashtags to functions

• Calls the function

• Fires the event

• Uses the “hashchange” event from browser or else polls the current location

12Wednesday, May 2, 2012

Page 13: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Router codevar Dispatcher = Backbone.Router.extend({ routes: {

“help”: “help”, “search/:query”: “search”},help: function(){},search: function(query){…});

<a href=”#/search/daVinci” />dispatcher1.search(“daVinci”);

Backbone.history.start(); //magic

13Wednesday, May 2, 2012

Page 14: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

More than one way to do itFor views: model.view or model.views[], or fire events, or create “controller” object to mediate, …

For sync: can sync per model, send sets of requests, …

For events: can mixin to any JS object, create custom events, …

For render: can use templates, direct DOM manip, jQuery UI, …

14Wednesday, May 2, 2012

Page 15: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

More than one place that’s using it

• LinkedIn Mobile

• Foursquare

• Khan Academy

• Groupon Now!

• Pandora

15Wednesday, May 2, 2012

Page 16: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

More than one place it’s documented

• http://documentcloud.github.com/backbone/

• http://documentcloud.github.com/backbone/docs/backbone.html

• http://backbonetutorials.com/

• http://blog.galk.me/post/17139384615/backbone-js-tutorial-create-a-simple-twitter-search

16Wednesday, May 2, 2012

Page 17: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Presented for your consideration:

• Takes care of basics of MVC

• Small, clean, & friendly code

• Flexible

• Reduces need to figure this stuff out yourself

17Wednesday, May 2, 2012

Page 18: Overview of Backbone

[email protected] Backbone-PhillyCoders-5/2/2012

Backbone.js

18Wednesday, May 2, 2012