Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nested REST resources support #39

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions packages/ember-data/lib/adapters/rest_adapter.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var get = Ember.get, set = Ember.set, getPath = Ember.getPath;

DS.RESTAdapter = DS.Adapter.extend({
parentResourceURL: '',

createRecord: function(store, type, model) {
var root = this.rootForType(type);

var data = {};
data[root] = get(model, 'data');

this.ajax("/" + this.pluralize(root), "POST", {
var url = [get(this, 'parentResourceURL'), this.pluralize(root)].join("/");

this.ajax(url, "POST", {
data: data,
success: function(json) {
store.didCreateRecord(model, json[root]);
Expand All @@ -28,7 +32,9 @@ DS.RESTAdapter = DS.Adapter.extend({
return get(model, 'data');
});

this.ajax("/" + this.pluralize(root), "POST", {
var url = [get(this, 'parentResourceURL'), this.pluralize(root)].join("/");

this.ajax(url, "POST", {
data: data,
success: function(json) {
store.didCreateRecords(type, models, json[plural]);
Expand All @@ -43,7 +49,7 @@ DS.RESTAdapter = DS.Adapter.extend({
var data = {};
data[root] = get(model, 'data');

var url = ["", this.pluralize(root), id].join("/");
var url = [get(this, 'parentResourceURL'), this.pluralize(root), id].join("/");

this.ajax(url, "PUT", {
data: data,
Expand All @@ -66,7 +72,9 @@ DS.RESTAdapter = DS.Adapter.extend({
return get(model, 'data');
});

this.ajax("/" + this.pluralize(root), "POST", {
var url = [get(this, 'parentResourceURL'), this.pluralize(root)].join("/");

this.ajax(url, "POST", {
data: data,
success: function(json) {
store.didUpdateRecords(models, json[plural]);
Expand All @@ -78,7 +86,7 @@ DS.RESTAdapter = DS.Adapter.extend({
var id = get(model, 'id');
var root = this.rootForType(type);

var url = ["", this.pluralize(root), id].join("/");
var url = [get(this, 'parentResourceURL'), this.pluralize(root), id].join("/");

this.ajax(url, "DELETE", {
success: function(json) {
Expand All @@ -101,7 +109,9 @@ DS.RESTAdapter = DS.Adapter.extend({
return get(model, primaryKey);
});

this.ajax("/" + this.pluralize(root) + "/delete", "POST", {
var url = [get(this, 'parentResourceURL'), this.pluralize(root), "/delete"].join("/");

this.ajax(url, "POST", {
data: data,
success: function(json) {
store.didDeleteRecords(models);
Expand All @@ -112,7 +122,7 @@ DS.RESTAdapter = DS.Adapter.extend({
find: function(store, type, id) {
var root = this.rootForType(type);

var url = ["", this.pluralize(root), id].join("/");
var url = [get(this, 'parentResourceURL'), this.pluralize(root), id].join("/");

this.ajax(url, "GET", {
success: function(json) {
Expand All @@ -124,19 +134,22 @@ DS.RESTAdapter = DS.Adapter.extend({
findMany: function(store, type, ids) {
var root = this.rootForType(type), plural = this.pluralize(root);

this.ajax("/" + plural, "GET", {
var url = [get(this, 'parentResourceURL'), plural].join("/");

this.ajax(url, "GET", {
data: { ids: ids },
success: function(json) {
store.loadMany(type, ids, json[plural]);
}
});
var url = "/" + plural;
},

findAll: function(store, type) {
var root = this.rootForType(type), plural = this.pluralize(root);

this.ajax("/" + plural, "GET", {
var url = [get(this, 'parentResourceURL'), plural].join("/");

this.ajax(url, "GET", {
success: function(json) {
store.loadMany(type, json[plural]);
}
Expand All @@ -146,7 +159,9 @@ DS.RESTAdapter = DS.Adapter.extend({
findQuery: function(store, type, query, modelArray) {
var root = this.rootForType(type), plural = this.pluralize(root);

this.ajax("/" + plural, "GET", {
var url = [get(this, 'parentResourceURL'), plural].join("/");

this.ajax(url, "GET", {
data: query,
success: function(json) {
modelArray.load(json[plural]);
Expand Down