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 queryRecord method to the RESTAdapter #3554

Merged
merged 1 commit into from
Jul 22, 2015
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
28 changes: 28 additions & 0 deletions packages/ember-data/lib/adapters/rest-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,34 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
return this.ajax(url, 'GET', { data: query });
},

/**
Called by the store in order to fetch a JSON object for
the record that matches a particular query.

The `queryRecord` method makes an Ajax (HTTP GET) request to a URL
computed by `buildURL`, and returns a promise for the resulting
payload.

The `query` argument is a simple JavaScript object that will be passed directly
to the server as parameters.

@private
@method queryRecord
@param {DS.Store} store
@param {DS.Model} type
@param {Object} query
@return {Promise} promise
*/
queryRecord: function(store, type, query) {
var url = this.buildURL(type.modelName, null, null, 'queryRecord', query);

if (this.sortQueryParams) {
query = this.sortQueryParams(query);
}

return this.ajax(url, 'GET', { data: query });
},

/**
Called by the store in order to fetch several records together if `coalesceFindRequests` is true

Expand Down
59 changes: 59 additions & 0 deletions packages/ember-data/tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,65 @@ test("findQuery - data is normalized through custom serializers", function() {
}));
});

test("queryRecord - returns a single record in an object", function() {
ajaxResponse({
post: {
id: '1',
name: 'Ember.js rocks'
}
});

store.queryRecord('post', { slug: 'ember-js-rocks' }).then(async(function(post) {
deepEqual(post.get('name'), "Ember.js rocks");
}));
});

test("queryRecord - returning sideloaded data loads the data", function() {
ajaxResponse({
post: { id: 1, name: "Rails is omakase" },
comments: [{ id: 1, name: "FIRST" }]
});

store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(async(function(post) {
var comment = store.peekRecord('comment', 1);

deepEqual(comment.getProperties('id', 'name'), { id: "1", name: "FIRST" });
}));
});

test("queryRecord - returning an array picks the first one but saves all records to the store", function() {
ajaxResponse({
post: [{ id: 1, name: "Rails is omakase" }, { id: 2, name: "Ember is js" }]
});

store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(async(function(post) {
var post2 = store.peekRecord('post', 2);

deepEqual(post.getProperties('id', 'name'), { id: "1", name: "Rails is omakase" });
deepEqual(post2.getProperties('id', 'name'), { id: "2", name: "Ember is js" });
}));
});

test("queryRecord - data is normalized through custom serializers", function() {
env.registry.register('serializer:post', DS.RESTSerializer.extend({
primaryKey: '_ID_',
attrs: { name: '_NAME_' }
}));

ajaxResponse({
post: { _ID_: 1, _NAME_: "Rails is omakase" }
});

store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(async(function(post) {

deepEqual(
post.getProperties('id', 'name'),
{ id: "1", name: "Rails is omakase" },
"Post 1 is loaded with correct data"
);
}));
});

test("findMany - findMany uses a correct URL to access the records", function() {
Post.reopen({ comments: DS.hasMany('comment', { async: true }) });
adapter.coalesceFindRequests = true;
Expand Down