On Github jheth / introduction-to-ember-data
Joe Heth
@jheth
http://github.com/jheth
Know how to talk to the server
window.Blog = Ember.Application.create(); Blog.ApplicationAdapter = DS.LSAdapter.extend({ namespace: 'blog-emberjs' });Can have model specific adapters
var Post = DS.Model.extend({ title: DS.attr('string'), body: DS.attr('string'), user: DS.belongsTo('user'), comments: DS.hasMany('comment'), createdAt: DS.attr('date'), isPublished: DS.attr('boolean', { default: false }) });
{ "post": {"title": 'My Post', "comment_ids": [1,2,3], "user_id": 1}, "comments": [ {'id': 1, ...}, {'id': 2, ...} ], "users": [ {'id': 1, ...} ] }
App.PostSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, { attrs: { comments: { embedded: 'always' } } });Explicit inverses
comments: DS.hasMany('comment', {async: true})
Embedded IDs, but no data.
{ "post": { "title": "Ember Data", "body": "How it works...", "comment_ids": [1,2,3] } }
Lazy Load Comments
post.get('comments') => GET "/comments?ids[]=1,ids[]=2,ids[]=3"
comments: DS.hasMany('comment', {async: true}) user: DS.belongsTo('user', {async: true})
// Links { "post": { "title": "Ember Data", "body": "How it works...", "links": { "comments": "/post/1/comments", "user": "/user/9" } }
Lazy Load Comments
post.get('comments') => GET "/post/1/comments" post.get('user') => GET "/user/9"
// Array this.store.find('post') GET "/posts" => [ {post: {id: 1, ...} }, {post: {id: 2, ...} } ] // Find Multiple this.store.find('post', {ids: [1,2,3]}); GET "/posts?ids[]=1&ids[]=2&ids[]=3" => [{},{},{}] // Query String parameters this.store.find('post', {hasComments: true, sort_by: 'created_at'}) GET "/posts?hasComments=true&sort_by=created_at" => [{post: ...}]
// Single Record this.store.find('post', 1) GET "/posts/1" => {post: {id: 1, ...} }
// Return local records var posts = this.store.all('post'); // => no network request
var post = this.store.createRecord('post', { title: 'Ember Data', body: 'Lorem ipsum' }); // Set relationship after object is loaded. this.store.find('user', 1).then(function(user) { post.set('author', user); });The store object is available in controllers and routes using this.store.
this.store.push('post', { id: 1, title: "Ember.js", body: "A javascript framework." }); // Patch var updateEvent = {id: 1, title: "New Title"}; this.store.update('post', updateEvent);
var pushData = { posts: [ {id: 1, post_title: "Great post", comment_ids: [2]} ], comments: [ {id: 2, comment_body: "Insightful comment"} ] } this.store.pushPayload(pushData);All objects in Ember must have an 'id' attribute.
var user = this.store.find('user', 9); // ...after the record has loaded user.incrementProperty('postCount'); user.set('isAdmin', true); person.get('isDirty'); // => true user.changedAttributes(); // => { isAdmin: [false, true], postCount: [2, 3] } user.save(); // => POST /users/9 // OR user.rollback(); // => reverts local state user.get('isDirty'); // => false // OR user.reload(); // => GET /users/9Promise is always returned
this.store.find('post', 1).then(function (post) { post.deleteRecord(); post.get('isDeleted'); // => true post.save(); // => DELETE /posts/1 }); // OR this.store.find('post', 2).then(function (post) { post.destroyRecord(); // => DELETE /posts/2 });
Removes from local store, no network request
// Remove single record this.store.find('post', 1).then(function(post) { post.unloadRecord(); // OR this.store.unloadRecord(post); }); // Remove all from local store. this.store.unloadAll( 'post' );
Ember Data's JSON deserializer looks for a meta key:
var result = this.store.find("post", { limit: 10, offset: 0 });
{ "posts": [ { "id": 1, "title": "My First Blog Post" }, { "id": 2, "title": "My Second Blog Post" } ], "meta": { "total": 100, "page": 0 } }
var meta = this.store.metadataFor("post"); // meta.total => 100 // Or you can access the metadata just for this query: var meta = result.get("content.meta");