From ed8e5b6cc4acf313b5ef7b6d85801b2d05c5d419 Mon Sep 17 00:00:00 2001 From: Eric Kelly Date: Sat, 6 Jun 2015 18:07:04 -0400 Subject: [PATCH] Replace/deprecate store.find(type) for store.findAll(type) --- README.md | 2 +- TRANSITION.md | 18 +++++++++--------- packages/ember-data/lib/system/adapter.js | 4 +--- packages/ember-data/lib/system/store.js | 6 +++--- .../integration/adapter/find-all-test.js | 8 ++++---- .../tests/integration/adapter/find-test.js | 19 +++++++++++++++++-- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ca2c00fae90..1854cf27007 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ Server](http://emberjs.com/guides/models/connecting-to-an-http-server/). From your route or controller: ```js -this.store.find('blogPost'); +this.store.findAll('blogPost'); ``` This returns a promise that resolves to the collection of records. diff --git a/TRANSITION.md b/TRANSITION.md index b088a762659..77320bc870f 100644 --- a/TRANSITION.md +++ b/TRANSITION.md @@ -28,7 +28,7 @@ Ember Data 1.0.beta.1: ```js App.PostsRoute = Ember.Route.extend({ model: function() { - return this.store.find('post'); + return this.store.findAll('post'); } }); @@ -180,7 +180,7 @@ App.NewPostRoute = Ember.Route.extend({ }); ``` -You also no longer need transactions to save relationships independently +You also no longer need transactions to save relationships independently of the model it belongs to, e.g. comments of a post. Ember Data 0.13: @@ -295,7 +295,7 @@ function retry(promise, retryCallback, nTimes) { }); } -// try to save the person up to 5 times +// try to save the person up to 5 times retry(person.save(), function() { return person.save(); }, 5); @@ -305,7 +305,7 @@ Because all async operations in Ember Data 1.0.beta.1 are promises, you can combine them together using normal promise primitives. ```js -this.store.find('person').then(function(people) { +this.store.findAll('person').then(function(people) { people.forEach(function(person) { person.set('isPaidUp', true); }); @@ -533,7 +533,7 @@ App.PostSerializer = DS.RESTSerializer.extend({ }); post.comments = commentIds; - + var post_payload = { post: post, comments: comments }; return this._super(store, type, post_payload, id); @@ -559,7 +559,7 @@ App.PostSerializer = DS.RESTSerializer.extend({ // below in `normalizeHash` var comments = payload._embedded.comments; post.comments = comments.mapBy('ID_'); - + var post_payload = { post: post, comments: comments }; return this._super(store, type, post_payload, id); @@ -620,7 +620,7 @@ App.PostSerializer = DS.RESTSerializer.extend({ // normalize the underscored properties for (var prop in hash) { - json[prop.camelize()] = hash[prop]; + json[prop.camelize()] = hash[prop]; } // delegate to any type-specific normalizations @@ -648,14 +648,14 @@ In 0.13, the REST Adapter automatically camelized incoming keys for you. It also expected `belongsTo` relationships to be listed under `name_id` and `hasMany` relationships to be listed under `name_ids`. -If your application returns json with underscored attributes and `_id` or `_ids` +If your application returns json with underscored attributes and `_id` or `_ids` for relation, you can extend `ActiveModelSerializer` and all will work out of the box. ```js App.ApplicationSerializer = DS.ActiveModelSerializer.extend({}); ``` -Note: DS.ActiveModelSerializer is not to be confused with the ActiveModelSerializer gem +Note: DS.ActiveModelSerializer is not to be confused with the ActiveModelSerializer gem that is part of Rails API project. A conventional Rails API project with produce underscored output and the `DS.ActiveModelSerializer` will perform the expected normalization behavior such as camelizing property keys in your JSON. diff --git a/packages/ember-data/lib/system/adapter.js b/packages/ember-data/lib/system/adapter.js index 4c4149e7b63..3af7cd8f4e1 100644 --- a/packages/ember-data/lib/system/adapter.js +++ b/packages/ember-data/lib/system/adapter.js @@ -124,8 +124,7 @@ var Adapter = Ember.Object.extend({ find: null, /** - The `findAll()` method is called when you call `find` on the store - without an ID (i.e. `store.find('post')`). + The `findAll()` method is used to retrieve all records for a given type. Example @@ -148,7 +147,6 @@ var Adapter = Ember.Object.extend({ }); ``` - @private @method findAll @param {DS.Store} store @param {DS.Model} type diff --git a/packages/ember-data/lib/system/store.js b/packages/ember-data/lib/system/store.js index 2159b60e1e7..8bd47325bd9 100644 --- a/packages/ember-data/lib/system/store.js +++ b/packages/ember-data/lib/system/store.js @@ -462,10 +462,10 @@ Store = Service.extend({ --- - To find all records for a type, call `find` with no additional parameters: + To find all records for a type, call `findAll`: ```javascript - store.find('person'); + store.findAll('person'); ``` This will ask the adapter's `findAll` method to find the records for the @@ -526,6 +526,7 @@ Store = Service.extend({ Ember.assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string'); if (arguments.length === 1) { + Ember.deprecate('Using store.find(type) has been deprecated. Use store.findAll(type) to retrieve all records for a given type.'); return this.findAll(modelName); } @@ -997,7 +998,6 @@ Store = Service.extend({ the array with records of that type. @method findAll - @private @param {String} modelName @return {DS.AdapterPopulatedRecordArray} */ diff --git a/packages/ember-data/tests/integration/adapter/find-all-test.js b/packages/ember-data/tests/integration/adapter/find-all-test.js index 1f4bfc6b727..846dcc58cd9 100644 --- a/packages/ember-data/tests/integration/adapter/find-all-test.js +++ b/packages/ember-data/tests/integration/adapter/find-all-test.js @@ -43,7 +43,7 @@ test("When all records for a type are requested, the store should call the adapt var allRecords; run(function() { - store.find('person').then(function(all) { + store.findAll('person').then(function(all) { allRecords = all; equal(get(all, 'length'), 1, "the record array's length is 1 after a record is loaded into it"); equal(all.objectAt(0).get('name'), "Braaaahm Dale", "the first item in the record array is Braaaahm Dale"); @@ -51,7 +51,7 @@ test("When all records for a type are requested, the store should call the adapt }); run(function() { - store.find('person').then(function(all) { + store.findAll('person').then(function(all) { // Only one record array per type should ever be created (identity map) strictEqual(allRecords, all, "the same record array is returned every time all records of a type are requested"); }); @@ -78,9 +78,9 @@ test("When all records for a type are requested, a rejection should reject the p var allRecords; run(function() { - store.find('person').then(null, function() { + store.findAll('person').then(null, function() { ok(true, "The rejection should get here"); - return store.find('person'); + return store.findAll('person'); }).then(function(all) { allRecords = all; equal(get(all, 'length'), 1, "the record array's length is 1 after a record is loaded into it"); diff --git a/packages/ember-data/tests/integration/adapter/find-test.js b/packages/ember-data/tests/integration/adapter/find-test.js index d07614e0ed4..e91de0e63b4 100644 --- a/packages/ember-data/tests/integration/adapter/find-test.js +++ b/packages/ember-data/tests/integration/adapter/find-test.js @@ -22,14 +22,12 @@ module("integration/adapter/find - Finding Records", { }); test("It raises an assertion when no type is passed", function() { - expectAssertion(function() { store.find(); }, "You need to pass a type to the store's find method"); }); test("It raises an assertion when `undefined` is passed as id (#1705)", function() { - expectAssertion(function() { store.find('person', undefined); }, "You may not pass `undefined` as id to the store's find method"); @@ -39,6 +37,23 @@ test("It raises an assertion when `undefined` is passed as id (#1705)", function }, "You may not pass `null` as id to the store's find method"); }); +test("store.find(type) is deprecated", function() { + env.registry.register('adapter:person', DS.Adapter.extend({ + findAll: function(store, typeClass) { + return []; + } + })); + + expectDeprecation( + function() { + run(function() { + store.find('person'); + }); + }, + 'Using store.find(type) has been deprecated. Use store.findAll(type) to retrieve all records for a given type.' + ); +}); + test("When a single record is requested, the adapter's find method should be called unless it's loaded.", function() { expect(2);