Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Optimizing an API for Ember Data (Boston)

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Optimizing an API for Ember Data (Boston)

I updated my talk from EmberCamp for a Boston Ember meetup. The slides include some updated code examples and fresh content.

Dan Gebhardt

March 06, 2013

Transcript

48 slides
  1. JSON underscore_naming include root element id: 1, fk_id: 1, fk_ids:

    [1] conventions for including related data AM::S Conventions
  2. DS.RESTAdapter Conventions JSON underscore_naming include root element id: 1, fk_id:

    1, fk_ids: [1] conventions for including related data
  3. DS.RESTAdapter Conventions JSON underscore_naming include root element id: 1, fk_id:

    1, fk_ids: [1] conventions for including related data IDENTICAL TO ActiveModel::Serializers
  4. One-to-Many Relationships App.Post = DS.Model.extend({ title: DS.attr('string'), comments: DS.hasMany('App.Comment') });

    App.Comment = DS.Model.extend({ body: DS.attr('string'), post: DS.belongsTo('App.Post') });
  5. class PostSerializer < ApplicationSerializer attributes :title has_many :comments end class

    CommentSerializer < ApplicationSerializer attributes :body belongs_to :post end One-to-Many Relationships
  6. { post: { id: 1, title: 'Ember is Omakase', comment_ids:

    [4, 5, 6] }, comments: [ {id: 4, post_id: 1, body: 'delicious!'}, {id: 5, post_id: 1, body: 'yuno turbolinks?'}, {id: 6, post_id: 1, body: 'is that a tentacle?'} ] } JSON One-to-Many Relationships
  7. One-to-One Relationships App.User = DS.Model.extend({ name: DS.attr('string'), rights: DS.belongsTo('App.Rights') });

    App.Rights = DS.Model.extend({ admin: DS.attr('boolean'), user: DS.belongsTo('App.User') });
  8. class UserSerializer < ApplicationSerializer attributes :id, :name has_one :rights end

    class RightsSerializer < ApplicationSerializer attributes :id, :admin belongs_to :user end One-to-One Relationships
  9. { user: { id: 1, name: '', rights_id: 2 },

    rights: [{ id: 2, admin: true, user_id: 1 }] } JSON One-to-One Relationships
  10. class UserSerializer < ApplicationSerializer attributes :id, :name has_one :rights end

    class RightsSerializer < ApplicationSerializer attributes :id, :admin end One-to-None Relationships
  11. { user: { id: 1, name: '', rights_id: 2 },

    rights: [{ id: 2, admin: true }] } JSON One-to-None Relationships
  12. Many-to-Many Relationships App.Post = DS.Model.extend({ title: DS.attr('string'), body: DS.attr('string'), tags:

    DS.hasMany('App.Tag') }); App.Tag = DS.Model.extend({ name: DS.attr('string'), posts: DS.hasMany('App.Post') });
  13. class PostSerializer < ApplicationSerializer attributes :id, :title, :body has_many :tags

    end class TagSerializer < ApplicationSerializer attributes :id, :name has_many :posts end Many-to-Many Relationships
  14. { posts: [ {id: 1, title: 'Hello world', tag_ids: [11,

    12]}, {id: 2, title: 'Goodbye', tag_ids: [11, 13]} ], tags: [ {id: 11, name: 'announcements', post_ids: [1,2]}, {id: 12, name: 'happy', post_ids: [1]}, {id: 13, name: 'sad', post_ids: [2]} ] } JSON Many-to-Many Relationships
  15. Many-to-None Relationships App.Post = DS.Model.extend({ title: DS.attr('string'), body: DS.attr('string'), tags:

    DS.hasMany('App.Tag') }); App.Tag = DS.Model.extend({ name: DS.attr('string') });
  16. class PostSerializer < ApplicationSerializer attributes :id, :title, :body has_many :tags

    end class TagSerializer < ApplicationSerializer attributes :id, :name end Many-to-None Relationships
  17. { posts: [ {id: 1, title: 'Hello world', tag_ids: [11,

    12]}, {id: 2, title: 'Goodbye', tag_ids: [11, 13]} ], tags: [ {id: 11, name: 'announcements'}, {id: 12, name: 'happy'}, {id: 13, name: 'sad'} ] } JSON Many-to-None Relationships
  18. { post: { id: 1, title: 'Ember is Omakase', comments:

    [ {id: 4, body: 'delicious!'}, {id: 5, body: 'yuno turbolinks?'}, {id: 6, body: 'is that a tentacle?'} ] } } JSON Serialized Embedded Data
  19. class PostSerializer < ApplicationSerializer attributes :title has_many :comments, embed: :objects

    end class CommentSerializer < ApplicationSerializer attributes :body belongs_to :post end Embedded Data Serializers
  20. Embedded Read-only Data App.Post = DS.Model.extend({ title: DS.attr('string'), comments: DS.hasMany('App.Comment')

    }); App.Comment = DS.Model.extend({ body: DS.attr('string') }); App.Adapter.map('App.Post', { comments: {embedded: 'load'} });
  21. Embedded Writeable Data App.Post = DS.Model.extend({ title: DS.attr('string'), comments: DS.hasMany('App.Comment')

    }); App.Comment = DS.Model.extend({ body: DS.attr('string') }); App.Adapter.map('App.Post', { comments: {embedded: 'always'} });
  22. { hobbit: {id: 1, name: 'Bilbo'} } { hobbitses: [

    {id: 1, name: 'Bilbo'} {id: 2, name: 'Frodo'} {id: 3, name: 'Samwise'} ] } JSON Custom Pluralization
  23. { post: { id: 1, title: 'Ember is Omakase', comment_ids:

    [4, 5, 6] }, post_comments: [ {id: 4, post_id: 1, body: 'delicious!'}, {id: 5, post_id: 1, body: 'yuno turbolinks?'}, {id: 6, post_id: 1, body: 'is that a tentacle?'} ] } JSON Custom Sideloading
  24. Custom Transforms App.Adapter.registerTransform('excitableString', { serialize: function(value) { return value +

    '!'; }, deserialize: function(value) { return value.substring(0, value.length - 1); } }); App.Greeting = DS.Model.extend({ message: DS.attr('excitableString') });
  25. Custom URLs App.Adapter.reopen({namespace: 'ember'}); person = store.find(Person, 1); // =>

    /ember/people/1 App.Adapter.reopen({url: 'http://api.ember.dev'}); person = store.find(Person, 1); // => http://api.ember.dev/people/1

More Decks by Dan Gebhardt

Other Decks in Programming