Skip to content

Commit

Permalink
Merge pull request #3234 from HeroicEric/make-find-all-public
Browse files Browse the repository at this point in the history
Make store.findAll(type) public
  • Loading branch information
bmac committed Jun 8, 2015
2 parents 7d2f848 + de36087 commit 8d7bcc0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions TRANSITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions packages/ember-data/lib/system/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -148,7 +147,6 @@ var Adapter = Ember.Object.extend({
});
```
@private
@method findAll
@param {DS.Store} store
@param {DS.Model} type
Expand Down
16 changes: 8 additions & 8 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -485,6 +485,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);
}

Expand Down Expand Up @@ -543,10 +544,8 @@ Store = Service.extend({
@return {Promise} promise
*/
fetchAll: function(modelName) {
Ember.assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string');
var typeClass = this.modelFor(modelName);

return this._fetchAll(typeClass, this.all(modelName));
Ember.deprecate('Using store.fetchAll(type) has been deprecated. Use store.findAll(type) to retrieve all records for a given type.');
return this.findAll(modelName);
},

/**
Expand Down Expand Up @@ -1014,13 +1013,14 @@ Store = Service.extend({
the array with records of that type.
@method findAll
@private
@param {String} modelName
@return {DS.AdapterPopulatedRecordArray}
*/
findAll: function(modelName) {
Ember.assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string');
return this.fetchAll(modelName);
var typeClass = this.modelFor(modelName);

return this._fetchAll(typeClass, this.all(modelName));
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ 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");
});
});

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");
});
Expand All @@ -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");
Expand Down
19 changes: 17 additions & 2 deletions packages/ember-data/tests/integration/adapter/find-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);

Expand Down
29 changes: 22 additions & 7 deletions packages/ember-data/tests/integration/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,28 @@ test("Using store#fetchById on existing record reloads it", function() {
});
});

module("integration/store - fetchAll", {
module("integration/store - findAll", {
setup: function() {
initializeStore(DS.RESTAdapter.extend());
}
});

test("Using store#fetchAll with no records triggers a query", function() {
test("store#fetchAll() is deprecated", function() {
ajaxResponse({
cars: []
});

expectDeprecation(
function() {
run(function() {
store.fetchAll('car');
});
},
'Using store.fetchAll(type) has been deprecated. Use store.findAll(type) to retrieve all records for a given type.'
);
});

test("Using store#findAll with no records triggers a query", function() {
expect(2);

ajaxResponse({
Expand All @@ -313,13 +328,13 @@ test("Using store#fetchAll with no records triggers a query", function() {
ok(!cars.get('length'), 'There is no cars in the store');

run(function() {
store.fetchAll('car').then(function(cars) {
store.findAll('car').then(function(cars) {
equal(cars.get('length'), 2, 'Two car were fetched');
});
});
});

test("Using store#fetchAll with existing records performs a query, updating existing records and returning new ones", function() {
test("Using store#findAll with existing records performs a query, updating existing records and returning new ones", function() {
expect(3);

run(function() {
Expand Down Expand Up @@ -347,15 +362,15 @@ test("Using store#fetchAll with existing records performs a query, updating exis
equal(cars.get('length'), 1, 'There is one car in the store');

run(function() {
store.fetchAll('car').then(function(cars) {
store.findAll('car').then(function(cars) {
equal(cars.get('length'), 2, 'There is 2 cars in the store now');
var mini = cars.findBy('id', '1');
equal(mini.get('model'), 'New Mini', 'Existing records have been updated');
});
});
});

test("store#fetchAll should return all known records even if they are not in the adapter response", function() {
test("store#findAll should return all known records even if they are not in the adapter response", function() {
expect(4);

run(function() {
Expand All @@ -375,7 +390,7 @@ test("store#fetchAll should return all known records even if they are not in the
equal(cars.get('length'), 2, 'There is two cars in the store');

run(function() {
store.fetchAll('car').then(function(cars) {
store.findAll('car').then(function(cars) {
equal(cars.get('length'), 2, 'It returns all cars');
var mini = cars.findBy('id', '1');
equal(mini.get('model'), 'New Mini', 'Existing records have been updated');
Expand Down

0 comments on commit 8d7bcc0

Please sign in to comment.