backbone identity map

21
Identity Maps & Backbone Ben Teese, Shine Technologies Thursday, 14 February 13

Upload: ben-teese

Post on 29-Nov-2014

577 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Backbone identity map

Identity Maps & BackboneBen Teese, Shine Technologies

Thursday, 14 February 13

Page 2: Backbone identity map

Overview

The Problem

About Identity Maps

Backbone.IdentityMap

Thursday, 14 February 13

Page 3: Backbone identity map

A Demo

Thursday, 14 February 13

Page 4: Backbone identity map

Thursday, 14 February 13

Page 5: Backbone identity map

Thursday, 14 February 13

Page 6: Backbone identity map

Thursday, 14 February 13

Page 7: Backbone identity map

The Problem

Roger

fetch

GET /suspects/1

Backend

fetch

GET /suspects

Collection ...

Todd

Michael

Roger

Thursday, 14 February 13

Page 8: Backbone identity map

What we want

Roger

fetch

GET /suspects/1

Backend

fetch

GET /suspects

Collection ...

Todd

Michael

Thursday, 14 February 13

Page 9: Backbone identity map

Identity Maps

Thursday, 14 February 13

Page 10: Backbone identity map

Identity Map

Todd

Michael

Roger

Fred

Suspect:1

Suspect:2

Suspect:3

Suspect:4

......

Thursday, 14 February 13

Page 11: Backbone identity map

Object Creation

if class and ID is in identity map return the cached objectelse create new object put it in identity map return the new objectend

Thursday, 14 February 13

Page 12: Backbone identity map

Backbone.IdentityMap

Backbone ‘Plugin’

Overrides Model Constructor

Does not alter original Backbone

Thursday, 14 February 13

Page 13: Backbone identity map

Before...

var Suspect = Backbone.Model.extend({ ...}); var Suspects = Backbone.Collection.extend({ model: Suspect, url: "/suspects"});

...

Thursday, 14 February 13

Page 14: Backbone identity map

After...

var Suspect = Backbone.IdentityMap( Backbone.Model.extend({ ... })); var Suspects = Backbone.Collection.extend({ model: Suspect, url: "/suspects"});

...

Thursday, 14 February 13

Page 15: Backbone identity map

Thursday, 14 February 13

Page 16: Backbone identity map

How does it work?

Backbone.IdentityMap = function(originalConstructor) { var newConstructor = _.extend(function(attributes, options) { // Execute our identity map logic ... return newOrCachedObject; }, originalConstructor);

return newConstructor;};

Thursday, 14 February 13

Page 17: Backbone identity map

How does it work with Backbone?

Thursday, 14 February 13

Page 18: Backbone identity map

Backbone.Collection..._prepareModel: function(model, options) { options || (options = {}); if (!(model instanceof Model)) { var attrs = model; options.collection = this; model = new this.model(attrs, options); if (!model._validate(model.attributes, options)) { model = false; } } else if (!model.collection) { model.collection = this; } return model;},...

Thursday, 14 February 13

Page 19: Backbone identity map

Backbone.Model

...// Create a new model with identical attributes to this one.clone: function() { return new this.constructor(this.attributes);},...

Thursday, 14 February 13

Page 20: Backbone identity map

Caveats

Maps take space

Logout should clear the map

Tests should clear the map

Can’t subclass (yet)

Thursday, 14 February 13

Page 21: Backbone identity map

github.com/shinetech

Thursday, 14 February 13