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

Rename adapter and serializer methods to match the new store methods #3306

Merged
merged 1 commit into from
Jun 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test("extractPolymorphic hasMany", function() {
var json;

run(function() {
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json_hash, '1', 'find');
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json_hash, '1', 'findRecord');
});

deepEqual(json, {
Expand Down Expand Up @@ -94,7 +94,7 @@ test("extractPolymorphic belongsTo", function() {
var json;

run(function() {
json = env.amsSerializer.normalizeResponse(env.store, DoomsdayDevice, json_hash, '1', 'find');
json = env.amsSerializer.normalizeResponse(env.store, DoomsdayDevice, json_hash, '1', 'findRecord');
});

deepEqual(json, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ test("normalizeSingleResponse", function() {

var json;
run(function() {
json = env.amsSerializer.normalizeSingleResponse(env.store, HomePlanet, json_hash, '1', 'find');
json = env.amsSerializer.normalizeSingleResponse(env.store, HomePlanet, json_hash, '1', 'findRecord');
});

deepEqual(json, {
Expand Down Expand Up @@ -210,7 +210,7 @@ test("extractPolymorphic hasMany", function() {
var json;

run(function() {
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json_hash, '1', 'find');
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json_hash, '1', 'findRecord');
});

deepEqual(json, {
Expand Down Expand Up @@ -251,7 +251,7 @@ test("extractPolymorphic belongsTo", function() {
var json;

run(function() {
json = env.amsSerializer.normalizeResponse(env.store, DoomsdayDevice, json_hash, '1', 'find');
json = env.amsSerializer.normalizeResponse(env.store, DoomsdayDevice, json_hash, '1', 'findRecord');
});

deepEqual(json, {
Expand Down Expand Up @@ -285,7 +285,7 @@ test("extractPolymorphic when the related data is not specified", function() {
};

run(function() {
json = env.amsSerializer.normalizeResponse(env.store, DoomsdayDevice, json, '1', 'find');
json = env.amsSerializer.normalizeResponse(env.store, DoomsdayDevice, json, '1', 'findRecord');
});

deepEqual(json, {
Expand Down Expand Up @@ -314,7 +314,7 @@ test("extractPolymorphic hasMany when the related data is not specified", functi
};

run(function() {
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json, '1', 'find');
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json, '1', 'findRecord');
});

deepEqual(json, {
Expand All @@ -336,7 +336,7 @@ test("extractPolymorphic does not break hasMany relationships", function() {
};

run(function () {
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json, '1', 'find');
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json, '1', 'findRecord');
});

deepEqual(json, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ test("extractSingle", function() {
});

run(function() {
env.store.find('super-villain', 1).then(function(minion) {
env.store.findRecord('super-villain', 1).then(function(minion) {
equal(minion.get('firstName'), "Tom");
});
});
Expand All @@ -182,7 +182,7 @@ test("extractArray", function() {
}]);

run(function() {
env.store.find('super-villain', 1).then(function(minion) {
env.store.findRecord('super-villain', 1).then(function(minion) {
equal(minion.get('firstName'), "Tom");
});
});
Expand Down
57 changes: 51 additions & 6 deletions packages/ember-data/lib/adapters/build-url-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var get = Ember.get;

```javascript
export default DS.Adapter.extend(BuildURLMixin, {
find: function(store, type, id, snapshot) {
var url = this.buildURL(type.modelName, id, snapshot, 'find');
findRecord: function(store, type, id, snapshot) {
var url = this.buildURL(type.modelName, id, snapshot, 'findRecord');
return this.ajax(url, 'GET');
}
});
Expand All @@ -27,7 +27,7 @@ var get = Ember.get;
@class BuildURLMixin
@namespace DS
*/
export default Ember.Mixin.create({
var BuildURLMixin = Ember.Mixin.create({
/**
Builds a URL for a given type and optional ID.

Expand All @@ -46,17 +46,23 @@ export default Ember.Mixin.create({
@param {(String|Array|Object)} id single id or array of ids or query
@param {(DS.Snapshot|Array)} snapshot single snapshot or array of snapshots
@param {String} requestType
@param {Object} query object of query parameters to send for findQuery requests.
@param {Object} query object of query parameters to send for query requests.
@return {String} url
*/
buildURL: function(modelName, id, snapshot, requestType, query) {
switch (requestType) {
case 'find':
// The `find` case is deprecated
return this.urlForFind(id, modelName, snapshot);
case 'findRecord':
return this.urlForFindRecord(id, modelName, snapshot);
case 'findAll':
return this.urlForFindAll(modelName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks backwards compatibility as well right? We should keep the old hooks in for 1.13 no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urlForFindRecord calls urlForFind if it looks like it has been overwritten https://github.com/emberjs/data/pull/3306/files#diff-fffd24b16fb23892703ed3ab601e6c80R123

case 'findQuery':
// The `findQuery` case is deprecated
return this.urlForFindQuery(query, modelName);
case 'query':
return this.urlForQuery(query, modelName);
case 'findMany':
return this.urlForFindMany(id, modelName, snapshot);
case 'findHasMany':
Expand Down Expand Up @@ -109,8 +115,22 @@ export default Ember.Mixin.create({
* @param {String} modelName
* @param {DS.Snapshot} snapshot
* @return {String} url
* @deprecated Use [urlForFindRecord](#method_urlForFindRecord) instead
*/
urlForFind: function(id, modelName, snapshot) {
urlForFind: urlForFind,

/**
* @method urlForFind
* @param {String} id
* @param {String} modelName
* @param {DS.Snapshot} snapshot
* @return {String} url
*/
urlForFindRecord: function(id, modelName, snapshot) {
if (this.urlForFind !== urlForFind) {
Ember.deprecate('BuildURLMixin#urlForFind has been deprecated and renamed to `urlForFindRecord`.');
return this.urlForFind(id, modelName, snapshot);
}
return this._buildURL(modelName, id);
},

Expand All @@ -128,8 +148,21 @@ export default Ember.Mixin.create({
* @param {Object} query
* @param {String} modelName
* @return {String} url
* @deprecated Use [urlForQuery](#method_urlForQuery) instead
*/
urlForFindQuery: function(query, modelName) {
urlForFindQuery: urlForFindQuery,

/**
* @method urlForQuery
* @param {Object} query
* @param {String} modelName
* @return {String} url
*/
urlForQuery: function(query, modelName) {
if (this.urlForFindQuery !== urlForFindQuery) {
Ember.deprecate('BuildURLMixin#urlForFindQuery has been deprecated and renamed to `urlForQuery`.');
return this.urlForFindQuery(query, modelName);
}
return this._buildURL(modelName);
},

Expand Down Expand Up @@ -270,3 +303,15 @@ export default Ember.Mixin.create({
return Ember.String.pluralize(camelized);
}
});

function urlForFind(id, modelName, snapshot) {
Ember.deprecate('BuildURLMixin#urlForFind has been deprecated and renamed to `urlForFindRecord`.');
return this._buildURL(modelName, id);
}

function urlForFindQuery(query, modelName) {
Ember.deprecate('BuildURLMixin#urlForFindQuery has been deprecated and renamed to `urlForQuery`.');
return this._buildURL(modelName);
}

export default BuildURLMixin;
68 changes: 61 additions & 7 deletions packages/ember-data/lib/adapters/rest-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ var RestAdapter = Adapter.extend(BuildURLMixin, {
relationships accessed within the same runloop. If you set `coalesceFindRequests: true`

```javascript
store.find('comment', 1);
store.find('comment', 2);
store.findRecord('comment', 1);
store.findRecord('comment', 2);
```

will also send a request to: `GET /comments?ids[]=1&ids[]=2`
Expand Down Expand Up @@ -346,26 +346,45 @@ var RestAdapter = Adapter.extend(BuildURLMixin, {

@property headers
@type {Object}
*/

/**
@method find
@param {DS.Store} store
@param {DS.Model} type
@param {String} id
@param {DS.Snapshot} snapshot
@return {Promise} promise
@deprecated Use [findRecord](#method_findRecord) instead
*/
find: function(store, type, id, snapshot) {
Ember.deprecate('RestAdapter#find has been deprecated and renamed to `findRecord`.');
return this.ajax(this.buildURL(type.modelName, id, snapshot, 'find'), 'GET');
},

/**
Called by the store in order to fetch the JSON for a given
type and ID.

The `find` method makes an Ajax request to a URL computed by `buildURL`, and returns a
promise for the resulting payload.
The `findRecord` method makes an Ajax request to a URL computed by
`buildURL`, and returns a promise for the resulting payload.

This method performs an HTTP `GET` request with the id provided as part of the query string.

@method find
@method findRecord
@param {DS.Store} store
@param {DS.Model} type
@param {String} id
@param {DS.Snapshot} snapshot
@return {Promise} promise
*/
find: function(store, type, id, snapshot) {
return this.ajax(this.buildURL(type.modelName, id, snapshot, 'find'), 'GET');
findRecord: function(store, type, id, snapshot) {
var find = RestAdapter.prototype.find;
if (find !== this.find) {
Ember.deprecate('RestAdapter#find has been deprecated and renamed to `findRecord`.');
return this.find(store, type, id, snapshot);
}
return this.ajax(this.buildURL(type.modelName, id, snapshot, 'findRecord'), 'GET');
},

/**
Expand Down Expand Up @@ -410,8 +429,10 @@ var RestAdapter = Adapter.extend(BuildURLMixin, {
@param {DS.Model} type
@param {Object} query
@return {Promise} promise
@deprecated Use [query](#method_query) instead
*/
findQuery: function(store, type, query) {
Ember.deprecate('RestAdapter#findQuery has been deprecated and renamed to `query`.');
var url = this.buildURL(type.modelName, null, null, 'findQuery', query);

if (this.sortQueryParams) {
Expand All @@ -421,6 +442,39 @@ var RestAdapter = Adapter.extend(BuildURLMixin, {
return this.ajax(url, 'GET', { data: query });
},

/**
Called by the store in order to fetch a JSON array for
the records that match a particular query.

The `findQuery` 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 query
@param {DS.Store} store
@param {DS.Model} type
@param {Object} query
@return {Promise} promise
*/
query: function(store, type, query) {
var findQuery = RestAdapter.prototype.findQuery;
if (findQuery !== this.findQuery) {
Ember.deprecate('RestAdapter#findQuery has been deprecated and renamed to `query`.');
return this.findQuery(store, type, query);
}

var url = this.buildURL(type.modelName, null, null, 'query', 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
21 changes: 12 additions & 9 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var merge = Ember.merge;
@namespace DS
@extends DS.Serializer
*/
export default Serializer.extend({
var JSONSerializer = Serializer.extend({

/**
The primaryKey is used when serializing and deserializing
Expand Down Expand Up @@ -231,8 +231,8 @@ export default Serializer.extend({
*/
normalizeResponse: function(store, primaryModelClass, payload, id, requestType) {
switch (requestType) {
case 'find':
return this.normalizeFindResponse(...arguments);
case 'findRecord':
return this.normalizeFindRecordResponse(...arguments);
case 'findAll':
return this.normalizeFindAllResponse(...arguments);
case 'findBelongsTo':
Expand All @@ -241,8 +241,8 @@ export default Serializer.extend({
return this.normalizeFindHasManyResponse(...arguments);
case 'findMany':
return this.normalizeFindManyResponse(...arguments);
case 'findQuery':
return this.normalizeFindQueryResponse(...arguments);
case 'query':
return this.normalizeQueryResponse(...arguments);
case 'createRecord':
return this.normalizeCreateRecordResponse(...arguments);
case 'deleteRecord':
Expand All @@ -253,15 +253,15 @@ export default Serializer.extend({
},

/*
@method normalizeFindResponse
@method normalizeFindRecordResponse
@param {DS.Store} store
@param {DS.Model} primaryModelClass
@param {Object} payload
@param {String|Number} id
@param {String} requestType
@return {Object} JSON-API Document
*/
normalizeFindResponse: function(store, primaryModelClass, payload, id, requestType) {
normalizeFindRecordResponse: function(store, primaryModelClass, payload, id, requestType) {
return this.normalizeSingleResponse(...arguments);
},

Expand Down Expand Up @@ -318,15 +318,15 @@ export default Serializer.extend({
},

/*
@method normalizeFindQueryResponse
@method normalizeQueryResponse
@param {DS.Store} store
@param {DS.Model} primaryModelClass
@param {Object} payload
@param {String|Number} id
@param {String} requestType
@return {Object} JSON-API Document
*/
normalizeFindQueryResponse: function(store, primaryModelClass, payload, id, requestType) {
normalizeQueryResponse: function(store, primaryModelClass, payload, id, requestType) {
return this.normalizeArrayResponse(...arguments);
},

Expand Down Expand Up @@ -1369,6 +1369,7 @@ export default Serializer.extend({
extractFind: function(store, typeClass, payload, id, requestType) {
return this.extractSingle(store, typeClass, payload, id, requestType);
},

/**
`extractFindBelongsTo` is a hook into the extract method used when
a call is made to `DS.Store#findBelongsTo`. By default this method is
Expand Down Expand Up @@ -1664,3 +1665,5 @@ function _newExtractMeta(store, modelClass, payload) {
return meta;
}
}

export default JSONSerializer;
Loading