Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
wecc committed Apr 22, 2015
1 parent da54b6e commit aa31e23
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
25 changes: 11 additions & 14 deletions packages/ember-data/lib/adapters/json-api-adapter.js
Expand Up @@ -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');
},

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/lib/serializers/json-api-serializer.js
Expand Up @@ -25,7 +25,7 @@ export default Serializer.extend({
},

normalizeTypeKey: function(typeKey) {
return Ember.String.camelize(typeKey);
return Ember.String.dasherize(typeKey);
},

serializeTypeKey: function(typeKey) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit aa31e23

Please sign in to comment.