Skip to content

Instantly share code, notes, and snippets.

@sandalsoft
Forked from wycats/app.js
Created May 28, 2014 21:58
Show Gist options
  • Save sandalsoft/601213e9959bf7467459 to your computer and use it in GitHub Desktop.
Save sandalsoft/601213e9959bf7467459 to your computer and use it in GitHub Desktop.
App.Router.map(function() {
this.resource('post', { path: '/posts/:post_id' });
});
App.PostRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('post', params.post_id);
}
});
// store stuff for all adapters in here
App.ApplicationAdapter = DS.Adapter.extend({
});
App.PostAdapter = App.ApplicationAdapter.extend({
find: function(store, type, id) {
return $.getJSON("/posts/" + id).then(function(json) {
// normalize JSON into "Ember Data Form"
return json;
});
}
});
/**
When the user navigates to /posts/1, Ember will run the model hook, which will call
store.find('post', "1"). This will, in turn, ask the Post adapter for Post 1. At this
point, you just do whatever async work you need to do to get the JSON.
Once you get the JSON, you just need to convert it into "Ember Data Form". What is
Ember Data Form, you ask?
{
// the ID must be named "id"
"id": <string>,
// attributes must be named exactly the same as they are named in your model
"attr1": <any>,
"attr2": <any>,
// has-many relationships must be named exactly as they are named in your model,
// and be an array of IDs
"comments": [ "5", "8", "34" ],
// belongs-to relationships must be named exactly as they are named in your model,
// and be a single ID.
"author": "12",
// alternatively, you can specify an URL to hit later. Currently, this is under
// "links"; it will soon be renamed to "_links"
"links": {
"comments": "/posts/1/comments",
"author": "/users/12"
}
}
*/
/**
If you specify a has-many relationship as an Array of IDs, it will work like this:
**/
App.Post = DS.Model.extend({
// Ember Data will automatically singularize `comments` to `comment` and use the CommentAdapter
comments: DS.hasMany()
});
App.CommentAdapter = App.ApplicationAdapter.extend({
findMany: function(store, type, ids) {
return $.getJSON("/comments", { ids: ids }).then(function(array) {
// normalize the array into an array of comment records in Ember Data Form
return array;
});
}
});
/**
A belongs-to works similarly, but it will invoke the regular `find` method on the relationship
**/
App.Post = DS.Model.extend({
// because author is not also the name of the model, you specify the name of the model here
author: DS.belongsTo('user')
});
App.UserAdapter = App.ApplicationAdapter.extend({
find: function(store, type, id) {
return $.getJSON("/users/" + id).then(function(json) {
// normal user into "Ember Data Form" and then return the normalized json
return json;
});
}
});
/**
You have probably noticed that there will be a lot of duplication if you have a somewhat sane
server API, and also that you'll have to do some extra work if your server includes additional data
alongside or embedded inside of the response. The RESTAdapter and ActiveModelAdapters provide more
defaults for those scenarios, with the cost of having to learn more about how those adapters work
and what they expect
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment