Slide 1

Slide 1 text

Data

Slide 2

Slide 2 text

• Core Concepts • Public API • Adapter/Serializer API • Internals

Slide 3

Slide 3 text

Why Ember Data?

Slide 4

Slide 4 text

Things It‘s Still Hard to Do in 2012 (which is bullshit)

Slide 5

Slide 5 text

Creating two records, setting up a relationship between them, then saving them.

Slide 6

Slide 6 text

Easily starting with fixture data if your server isn‘t ready yet.

Slide 7

Slide 7 text

Changing the JSON representation of your model without changing app code.

Slide 8

Slide 8 text

Only loading models you don‘t have cached.

Slide 9

Slide 9 text

Keeping the DOM up-to-date with model changes.

Slide 10

Slide 10 text

Dynamically switching between a server and offline cache.

Slide 11

Slide 11 text

Using awesome new browser features like WebSockets and TypedArrays.

Slide 12

Slide 12 text

These problems are SO COMMON!

Slide 13

Slide 13 text

Why do we all do it by hand?!

Slide 14

Slide 14 text

Convention Over Configuration

Slide 15

Slide 15 text

conventional conventional { json: "lol" } WHY IS THIS NOT CONVENTIONAL?!?!

Slide 16

Slide 16 text

Ember Data

Slide 17

Slide 17 text

Architecture Overview

Slide 18

Slide 18 text

Store Adapter Serializer Record Record Record Record

Slide 19

Slide 19 text

Store Adapter Serializer Record Record Record Record Server Semantics Application Semantics Things that wouldn't change if your backend changed. I.e., ignoring your server protocol, how would you ideally model your data? How do you think about your models? Things that are specific to your server/backend/ persistence layer.

Slide 20

Slide 20 text

Loading a Record

Slide 21

Slide 21 text

Store Application App.Post.find(1) Record

Slide 22

Slide 22 text

Store Application Person.find([1, 2, 3]) RecordArray

Slide 23

Slide 23 text

Store Application Person.find() RecordArray

Slide 24

Slide 24 text

// State for '/posts' App.PostsState = Ember.State.extend({ setupControllers: function() { var posts = App.Post.find(); this.set('controller.content', posts); } });

Slide 25

Slide 25 text

Store Application App.Post.find(1) Adapter Record loading

Slide 26

Slide 26 text

Store Adapter Record loading

Slide 27

Slide 27 text

Store Record Adapter Serializer loading

Slide 28

Slide 28 text

Store Record Adapter Serializer loaded

Slide 29

Slide 29 text

Store Application App.Post.find(1) Record loaded

Slide 30

Slide 30 text

Ember Data in Practice

Slide 31

Slide 31 text

Setup

Slide 32

Slide 32 text

App.Store = DS.Store.extend();

Slide 33

Slide 33 text

var attr = DS.attr; App.Post = DS.Model.extend({ title: attr('string'), body: attr('string'), comments: DS.hasMany('App.Comment'), author: DS.belongsTo('App.User') });

Slide 34

Slide 34 text

Finding Records

Slide 35

Slide 35 text

var person = App.Person.find(1);

Slide 36

Slide 36 text

person.get('isLoaded'); //=> false person.get('firstName'); //=> undefined

Slide 37

Slide 37 text

wait a bit

Slide 38

Slide 38 text

person.get('isLoaded'); //=> true person.get('firstName'); //=> "Tywin"

Slide 39

Slide 39 text

Changing Records

Slide 40

Slide 40 text

var tyrion = App.Person.find(2); tyrion.get('isDirty'); //=> false tyrion.get('firstName'); //=> "Tyrion" tyrion.set('firstName', "Yollo"); tyrion.get('isDirty'); //=> true

Slide 41

Slide 41 text

store.commit(); tyrion.get('isDirty'); //=> true tyrion.get('isSaving'); //=> true

Slide 42

Slide 42 text

wait a bit

Slide 43

Slide 43 text

tyrion.get('isDirty'); //=> false tyrion.get('isSaving'); //=> false

Slide 44

Slide 44 text

Creating Records

Slide 45

Slide 45 text

var eddard = App.Person.createRecord({ firstName: "Eddard", lastName: "Stark" }); var robb = App.Person.createRecord({ firstName: "Robb", lastName: "Stark" });

Slide 46

Slide 46 text

robb.set('parent', eddard); store.commit();

Slide 47

Slide 47 text

Deleting Records

Slide 48

Slide 48 text

tywin.deleteRecord();

Slide 49

Slide 49 text

Transactions

Slide 50

Slide 50 text

var transaction = store.transaction(); transaction.add(tyrion); transaction.add(tywin); transaction.commit();

Slide 51

Slide 51 text

transaction.rollback();

Slide 52

Slide 52 text

Record States

Slide 53

Slide 53 text

Loaded Updated Created Deleted Clean Dirty In-Flight In-Flight Error Error

Slide 54

Slide 54 text

var person = App.Person.find(1);

Slide 55

Slide 55 text

person.get('isLoaded'); //=> false person.get('firstName'); //=> undefined

Slide 56

Slide 56 text

wait a bit

Slide 57

Slide 57 text

person.get('isLoaded'); //=> true person.get('firstName'); //=> "Tywin"

Slide 58

Slide 58 text

isLoaded isDirty isSaving isDeleted isError isNew isValid These are derived from the current state object, which is awesome!

Slide 59

Slide 59 text

• Each record has an associated state manager. • The store does not make changes to records directly. • Instead, events are sent to the record. • The record responds based on its current state.

Slide 60

Slide 60 text

Uncaught Error: could not respond to event setProperty in state rootState.loaded.created.inFlight. = You tried to change a record while it was being saved.

Slide 61

Slide 61 text

// store.js record.loadedData();

Slide 62

Slide 62 text

// model/model.js loadedData: function() { this.send('loadedData'); }

Slide 63

Slide 63 text

// model/states.js loading: DS.State.create({ // TRANSITIONS exit: function(manager) { var record = get(manager, 'record'); record.trigger('didLoad'); }, // EVENTS loadedData: function(manager) { didChangeData(manager); manager.transitionTo('loaded'); } })

Slide 64

Slide 64 text

Adapters

Slide 65

Slide 65 text

• Are relationships saved in the parent or the child? • What payloads are sent to what URLs? • What actions map to what HTTP verbs? • What is the name of the primary key? • What are the names of attributes? • Are objects embedded or referred to by ID? Serializer Adapter

Slide 66

Slide 66 text

Serializer JSONSerializer RESTSerializer

Slide 67

Slide 67 text

Serializer Transform Values new Date() to "2007-04-05T14:30" (and vice versa)

Slide 68

Slide 68 text

DS.JSONSerializer.registerTransform('boolean', { serialize: function(value) { return value ? 'true' : 'false'; }, deserialize: function(value) { return value === 'false' ? false : true; } }); Serializer

Slide 69

Slide 69 text

Serializer Add ID addId: function(data, key, id) { data[key] = id; }

Slide 70

Slide 70 text

Serializer Add Attributes addAttribute: function(hash, key, value) { hash[key] = value; }

Slide 71

Slide 71 text

Serializer Add Belongs-To Relationships addBelongsTo: function(hash, record, key, relationship) { var id = get(record, relationship.key+'.id'); if (!Ember.none(id)) { hash[key] = id; } }

Slide 72

Slide 72 text

Serializer Extract Attributes extractAttribute: function(type, hash, attributeName) { var key = this._keyForAttributeName(type, attributeName); return hash[key]; }

Slide 73

Slide 73 text

Serializer Declarative Syntax App.Adapter.map('Person', { firstName: { key: 'FIRST_NAME' } });

Slide 74

Slide 74 text

Thank you. Questions? http://plus.tomdale.net http://emberjs.com @tomdale