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

Make RESTAdapter#findAll use collectionKey only when present #38

Merged
merged 1 commit into from
May 17, 2013
Merged
Show file tree
Hide file tree
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
Make RESTAdapter#findAll use record.constructor.collectionKey only wh…
…en present
  • Loading branch information
ahaurw01 committed May 15, 2013
commit 489e2cf9903f08892cc0bbd4901a7a227bd697b1
7 changes: 2 additions & 5 deletions packages/ember-model/lib/rest_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ Ember.RESTAdapter = Ember.Adapter.extend({
var url = this.buildURL(klass),
collectionKey = klass.collectionKey;

if (!collectionKey) {
throw new Error('Ember.RESTAdapter requires a `collectionKey` property to be specified');
}

return this.ajax(url).then(function(data) {
Ember.run(records, records.load, klass, data[collectionKey]);
var dataToLoad = collectionKey ? data[collectionKey] : data;
Ember.run(records, records.load, klass, dataToLoad);
});
},

Expand Down
54 changes: 44 additions & 10 deletions packages/ember-model/tests/adapter/rest_adapter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ module("Ember.RESTAdapter - with a url specified", {
}
});

test("findAll throws an error if collectionKey isn't specified", function() {
expect(1);

RESTModel.collectionKey = null;

throws(function() {
Ember.run(RESTModel, RESTModel.find);
}, /requires a `collectionKey` property to be specified/);
});

test("findAll", function() {
expect(3);

Expand All @@ -66,6 +56,50 @@ test("findAll", function() {
Ember.run(RESTModel, RESTModel.find);
});

test("findAll loads the full JSON payload when collectionKey isn't specified", function() {
expect(1);

var data = [
{id: 1, name: 'Erik'},
{id: 2, name: 'Aaron'}
],
records;
RESTModel.collectionKey = undefined;

adapter._ajax = function(url, params, method) {
return ajaxSuccess(data);
};

Ember.run(function() {
records = RESTModel.findAll();
});

equal(records.get('length'), data.length, "The proper number of items should have been loaded.");
});

test("findAll loads the proper JSON payload subset when collectionKey is specified", function() {
expect(1);

var data = {
posts: [
{id: 1, name: 'Erik'},
{id: 2, name: 'Aaron'}
]
},
records;
RESTModel.collectionKey = "posts";

adapter._ajax = function(url, params, method) {
return ajaxSuccess(data);
};

Ember.run(function() {
records = RESTModel.findAll();
});

equal(records.get('length'), data.posts.length, "The proper number of items should have been loaded.");
});

test("findById", function() {
expect(4);

Expand Down