diff --git a/packages/ember-data/lib/adapters/json-api-adapter.js b/packages/ember-data/lib/adapters/json-api-adapter.js index 7e79259137d..a88de3869f3 100644 --- a/packages/ember-data/lib/adapters/json-api-adapter.js +++ b/packages/ember-data/lib/adapters/json-api-adapter.js @@ -57,29 +57,26 @@ export default Adapter.extend(BuildURLMixin, { findBelongsTo: function(store, snapshot, link, relationship) { - var url, id, type; - - if (link.related) { - url = link.related; - } else { - id = snapshot.id; - type = snapshot.typeKey; - url = this.buildURL(type, id, null, 'findBelongsTo'); - } - - return this.ajax(url, 'GET'); + return this._findRelationship(store, snapshot, link, relationship, 'findBelongsTo'); }, findHasMany: function(store, snapshot, link, relationship) { - var url; //, id, type, host; + return this._findRelationship(store, snapshot, link, relationship, 'findHasMany'); + }, + + _findRelationship: function(store, snapshot, link, relationship, requestType) { + var url; + var type = snapshot.typeKey; + var id = snapshot.id; if (link.related) { url = link.related; } else { - // TODO - return; + url = Ember.String.dasherize(relationship.key); } + url = this.urlPrefix(url, this.buildURL(type, id, null, requestType)); + return this.ajax(url, 'GET'); }, diff --git a/packages/ember-data/lib/serializers/json-api-serializer.js b/packages/ember-data/lib/serializers/json-api-serializer.js index 61fcc8bbf8e..1e5cb301ce6 100644 --- a/packages/ember-data/lib/serializers/json-api-serializer.js +++ b/packages/ember-data/lib/serializers/json-api-serializer.js @@ -25,7 +25,7 @@ export default Serializer.extend({ }, normalizeTypeKey: function(typeKey) { - return Ember.String.camelize(typeKey); + return Ember.String.dasherize(typeKey); }, serializeTypeKey: function(typeKey) { diff --git a/packages/ember-data/tests/integration/adapter/json-api-adapter-test.js b/packages/ember-data/tests/integration/adapter/json-api-adapter-test.js index 23ba5f761ef..ef1b6810963 100644 --- a/packages/ember-data/tests/integration/adapter/json-api-adapter-test.js +++ b/packages/ember-data/tests/integration/adapter/json-api-adapter-test.js @@ -57,11 +57,11 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', { post: Post, comment: Comment, handle: Handle, - githubHandle: GithubHandle, - twitterHandle: TwitterHandle, + 'github-handle': GithubHandle, + 'twitter-handle': TwitterHandle, company: Company, - developmentShop: DevelopmentShop, - designStudio: DesignStudio + 'development-shop': DevelopmentShop, + 'design-studio': DesignStudio }); store = env.store; @@ -239,6 +239,48 @@ test('find a single record with belongsTo link as string', function() { }); }); +test('find a single record with belongsTo link as object { self }', function() { + expect(7); + + ajaxResponse([{ + data: { + type: "post", + id: "1", + title: "Ember.js rocks", + author: "2", + links: { + author: { + self: "dummy" + } + } + } + }, { + data: { + type: "user", + id: "2", + 'first-name': 'Yehuda', + 'last-name': 'Katz' + } + }]); + + run(function() { + store.find('post', 1).then(function(post) { + equal(passedUrl[0], '/post/1'); + + equal(post.get('id'), '1'); + equal(post.get('title'), 'Ember.js rocks'); + + post.get('author').then(function(author) { + equal(passedUrl[1], '/post/1/author'); + + equal(author.get('id'), '2'); + equal(author.get('firstName'), 'Yehuda'); + equal(author.get('lastName'), 'Katz'); + }); + }); + }); +}); + test('find a single record with belongsTo link as object { related }', function() { expect(7); @@ -447,6 +489,50 @@ test('find a single record with hasMany link as string', function() { }); }); +test('find a single record with hasMany link as object { self }', function() { + expect(7); + + ajaxResponse([{ + data: { + type: "post", + id: "1", + title: "Ember.js rocks", + links: { + comments: { + self: "dummy" + } + } + } + }, { + data: [{ + type: "comment", + id: "2", + text: "This is the first comment" + }, { + type: "comment", + id: "3", + text: "This is the second comment" + }] + }]); + + run(function() { + store.find('post', 1).then(function(post) { + equal(passedUrl[0], '/post/1'); + + equal(post.get('id'), '1'); + equal(post.get('title'), 'Ember.js rocks'); + + post.get('comments').then(function(comments) { + equal(passedUrl[1], '/post/1/comments'); + + equal(comments.get('length'), 2); + equal(comments.get('firstObject.text'), 'This is the first comment'); + equal(comments.get('lastObject.text'), 'This is the second comment'); + }); + }); + }); +}); + test('find a single record with hasMany link as object { related }', function() { expect(7);