ember data introduction and basic concepts

13
GoodData Confidential. 2013 GoodData Corporation. All rights reserved. Ember Data Adam Kloboučník, GoodData @akloboucnik

Upload: adam-kloboucnik

Post on 08-May-2015

392 views

Category:

Technology


3 download

DESCRIPTION

Slides for talk given on November PragueJS meetup (http://www.praguejs.cz/talks/#november-2013)

TRANSCRIPT

Page 1: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Ember DataAdam Kloboučník, GoodData@akloboucnik

Page 2: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Ember Data

▶ client data-persistence library▶ for Ember.js

▶ still beta-quality▶ strongly opinionated (naming, url + model

schema)▶ “trivial choices are enemy” ~ @wycats

Page 3: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Complications

Data

Web Applications

merge problems

Page 4: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Complications

User

Post

Comment

changes

dirty checking

Page 5: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Complications

▶ merge problems▶ dirty checking▶ async

Page 6: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Ember Data architecture

DataREST API

LocalStorage

Google Spreadsheet

Adapter

Serializer

Store

“Talks” protocol

Extracts raw data to Models

Persists models

Models

Page 7: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

ModelsApp.Post = DS.Model.extend({

title: DS.attr('string'),

body: DS.attr('string'),

comments: DS.hasMany('comment', {async:

true})

});

App.Comment = DS.Model.extend({

content: DS.attr('string'),

post: DS.belongsTo('post')

});

Page 8: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Adapter

▶ provides protocol abstraction▶ RESTAdapter, FixtureAdapter provided▶ LocalStorageAdapter, many others...

Page 9: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Intermezzo - promises

▶ “eventual value returned from completion of operation”function callApi(id) {

var promise = longRunningOperation(id);

return promise;

}

//....

callApi(2).then(function(result) {

// do sth with resolved result

}, function(err) {

// do sth with rejection

});

Page 10: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Store

▶ querying and saving▶ store.find[All,Query](‘model-name’[, id]);▶ model_object.save();▶ both finders and save return promises for developer

sanity (formerly a state chart with actions on edges)▶ Em.RSVP.all([promise1, promise2]).then ...

Page 11: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Notes

▶ identity map▶ sideloading▶ optimize you API for easier life (jsonapi.org)

▶ active_model_serializers in Rails▶ :-( elsewhere

▶ alternatives▶ Ember.Resource▶ Ember.Model▶ EPF

▶ sources - emberjs.com/guides, source

Page 12: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

▶ really young now, but▶ provides clean separation of concerns

▶ developing client app without backend▶ testing adapter code without rest of the app▶ …

▶ “best practices” for api modelling▶ integrates greatly with Ember if you use it

Conclusion

Page 13: Ember Data Introduction and Basic Concepts

GoodData Confidential. 2013 GoodData Corporation. All rights reserved.

Thank you!

@akloboucnik