Skip to content

Instantly share code, notes, and snippets.

@cullymason
Last active December 22, 2015 16:09
Show Gist options
  • Save cullymason/6497717 to your computer and use it in GitHub Desktop.
Save cullymason/6497717 to your computer and use it in GitHub Desktop.
Ember Restful Ember-Data adapter

A Brief Explaination

I wish there were an ember-data adapter that would use the url instead of using query strings for looking up related models

For example pull from url/model/model_id/releatedModel/ instead of pulling from url/relatedModel?ids="3,4,5"

An example

You have a company with different departments filled with employees serving different roles in the company. So you would have models like this in Ember:

Department Model

App.Department = DS.Model.extend({
	name: DS.attr('string'),
	employees: DS.hasMany('employee')
})

Employee Model

App.Employee = DS.Model.extend({
	department: DS.belongsTo('department'),
	name: DS.attr('string'),
	roles: DS.hasMany('role')
})

Role Model

App.Role = DS.Model.extend({
	name: DS.attr('string')
})

You could use

url/department/2/employees/
url/employee/43/roles/

instead of

url/employees?ids=...
url/roles?ids=...

In Conclusion

I think an added benefit would be that in the JSON, you would not need to require the ids of the related Models (they could just be option)

I have no experience writing adapters but, would this be difficult to achieve?

@cullymason
Copy link
Author

Heres an update:

@cullymason tl;dr normalize your JSON to have: { links: { comments: url } } when you push and it will "just work"

— Yehuda Katz (@wycats) September 9, 2013
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment