Slide 1

Slide 1 text

An Ember.js adapter for Django Diego Muñoz Escalante PyCon Canada 2012

Slide 2

Slide 2 text

JS? This is PyCon! The Shining. 1980. Stanley Kubrick

Slide 3

Slide 3 text

Business is business

Slide 4

Slide 4 text

The API Features (from http://tastypieapi.org): ● Full GET/POST/PUT/DELETE/PATCH support ● Reasonable defaults ● Designed to be extended at every turn ● Includes a variety of serialization formats (JSON/XML/YAML/bplist) ● HATEOAS by default ● Well-tested & well-documented

Slide 5

Slide 5 text

The API: Django Models

Slide 6

Slide 6 text

The API: Tastypie Resources

Slide 7

Slide 7 text

Choosing a client ● Gift oriented application ○ Smartphone client ○ Tablet in associated venues ● Requirements ○ Share as much code as possible ○ Multiplatform ○ Have fun (web open technologies)

Slide 8

Slide 8 text

Choosing a client ● Gift oriented application ○ Smartphone client ○ Tablet in associated venues ● Requirements ○ Share as much code as possible ○ Multiplatform ○ Have fun (web open technologies)

Slide 9

Slide 9 text

The client: Ember.js Features: ● More productive out of the box ● Conventions and idioms ● Data bindings ● "Magic" templates ● Computed properties ● Router ● Handlebars ● Persistence ● Classes, inheritance, mixins ● Community http://emberjs.com

Slide 10

Slide 10 text

ember-data http://github.com/emberjs/data ● Easy way to: ○ Read models from persistence layer ○ Create, update and delete them ● Perfect option to connect to Tastypie ○ Create models in ember-data ○ Connect with the API

Slide 11

Slide 11 text

ember-data: models Plain Javascript models? Backbone models?

Slide 12

Slide 12 text

ember-data: models

Slide 13

Slide 13 text

ember-data: REST adapter ● find() ● createRecord() ● updateRecord() ● deleteRecord() Everything is REST Problem solved!

Slide 14

Slide 14 text

ember-data: REST adapter ● find() ● createRecord() ● updateRecord() ● deleteRecord() Everything is REST Problem solved! NO :(

Slide 15

Slide 15 text

ember-data & tastypie problems Tastypie Ember-Data expects {"name": "Test the stress machine", "person_id": "/api/v1/person/1"} {"name": "Test the stress machine", "person_id": "1"} {"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"id": "1", "name": "Task1", "person_id": "/api/v1/person/1/", "resource_uri": "/api/v1/task/1/"]} {"tasks": [ {"id": "1", "name": "Task1", "person_id": "1"] } /api/v1/task /tasks No data returned by default Requires data returned

Slide 16

Slide 16 text

ember-data & tastypie problems http://nooooooooooooooo.com/

Slide 17

Slide 17 text

ember-data & tastypie problems What should I change? 1. Tastypie settings a. What about other clients connected to the API? b. It is already working, I don't want to modify it 2. Ember-data a. I love Python. Ember is a really complex piece of JS b. Breaking the conventions is a bad idea

Slide 18

Slide 18 text

ember-data & tastypie problems What should I change? 1. Tastypie settings a. What about other clients connected to the API? b. It is already working, I don't want to modify it 2. Ember-data a. I love Python. Ember is a really complex piece of JS b. Breaking the conventions is a bad idea Answer: I shouldn't change anything

Slide 19

Slide 19 text

ember-data-tastypie-adapter https://github.com/escalant3/ember-data-tastypie-adapter It does all the dirty work. Overrides: ● Adapter: init, createRecord, updateRecord, deleteRecord, didCreateRecord, didUpdateRecord, didDeleteRecord, didFindRecord, findMany, didFindMany, didFindAll, didFindQuery, buildURL, pluralize ● Serializer: keyForBelongsTo, keyForAttributeName, addBelongsTo, addHasMany, extractHasMany, extractBelongsTo

Slide 20

Slide 20 text

ember-data-tastypie-adapter How do I use this?

Slide 21

Slide 21 text

ember-data-tastypie-adapter Configuring the Python side

Slide 22

Slide 22 text

ember-data-tastypie-adapter Configuring the client

Slide 23

Slide 23 text

Examples

Slide 24

Slide 24 text

Examples

Slide 25

Slide 25 text

Examples

Slide 26

Slide 26 text

Examples

Slide 27

Slide 27 text

Examples Advanced features: - Transactions - Since (not available yet) - Handlebars ready - Customizable queries - Accesible record states - As advanced as you want

Slide 28

Slide 28 text

Things to do Ember community ● Ember.js 1.0 ● ember-data is an external component (yet) ● ember-data is under heavy development ● Missing features: ○ Error handling ○ Embedded data ○ ManyToMany https://github.com/emberjs/ember.js https://github.com/emberjs/data #emberjs

Slide 29

Slide 29 text

Things to do ember-data-tastypie-adapter ● Deletion of objects with associated data ● ManyToMany workaround ● Missing functionalities from RESTAdapter ● Community ○ Are you using the adapter? ○ Did you have problems? ○ Share your hacks

Slide 30

Slide 30 text

That's all! Project: https://github.com/escalant3/ember-data-tastypie-adapter https://github.com/escalant3/django-ember-example Contact: @escalant3 Thanks!