Skip to content

Commit

Permalink
Created store.fetchById and store.fetchAll.
Browse files Browse the repository at this point in the history
* `store.fetchById` behaves like `store.fetch` used to.
* `store.fetchAll` behaves like `store.find` called with only a type.
* `store.fetch` is aliased to `store.fetchById` and deprecated.
  • Loading branch information
cibernox committed Jan 31, 2015
1 parent c1ea7f0 commit 674678c
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 15 deletions.
39 changes: 32 additions & 7 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,20 +515,47 @@ Store = Ember.Object.extend({
});
```
@method fetch
@method fetchById
@param {String or subclass of DS.Model} type
@param {String|Integer} id
@param {Object} preload - optional set of attributes and relationships passed in either as IDs or as actual models
@return {Promise} promise
*/
fetch: function(type, id, preload) {
fetchById: function(type, id, preload) {
if (this.hasRecordForId(type, id)) {
return this.getById(type, id).reload();
} else {
return this.find(type, id, preload);
}
},

/**
This method returns a fresh collection from the server, regardless of if there is already records
in the store or not.
@method fetchAll
@param {String or subclass of DS.Model} type
@return {Promise} promise
*/
fetchAll: function(type) {
type = this.modelFor(type);

return this._fetchAll(type, this.all(type));
},

/**
@method fetch
@param {String or subclass of DS.Model} type
@param {String|Integer} id
@param {Object} preload - optional set of attributes and relationships passed in either as IDs or as actual models
@return {Promise} promise
@deprecated Use [fetchById](#method_fetchById) instead
*/
fetch: function(type, id, preload) {
Ember.deprecate('Using store.fetch() has been deprecated. Use store.fetchById for fetching individual records or store.fetchAll for collections');
return this.fetchById(type, id, preload);
},

/**
This method returns a record for a given type and id combination.
Expand Down Expand Up @@ -899,19 +926,17 @@ Store = Ember.Object.extend({
@return {DS.AdapterPopulatedRecordArray}
*/
findAll: function(typeName) {
var type = this.modelFor(typeName);

return this.fetchAll(type, this.all(type));
return this.fetchAll(typeName);
},

/**
@method fetchAll
@method _fetchAll
@private
@param {DS.Model} type
@param {DS.RecordArray} array
@return {Promise} promise
*/
fetchAll: function(type, array) {
_fetchAll: function(type, array) {
var adapter = this.adapterFor(type);
var sinceToken = this.typeMapFor(type).metadata.since;

Expand Down
139 changes: 131 additions & 8 deletions packages/ember-data/tests/integration/store_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,51 @@ function ajaxResponse(value) {
};
}

test("Using store#fetch on existing record reloads it", function() {
test("Using store#fetch is deprecated", function() {
ajaxResponse({
cars: [
{ id: 1, make: 'BMW', model: 'Mini' }
]
});

expectDeprecation(
function() {
run(function() {
store.fetch('car', 1);
});
},
'Using store.fetch() has been deprecated. Use store.fetchById for fetching individual records or store.fetchAll for collections'
);
});

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

test("Using store#fetchById on non existing record fetches it from the server", function() {
expect(2);

ajaxResponse({
cars: [{
id: 20,
make: 'BMCW',
model: 'Mini'
}]
});

var car = store.hasRecordForId('car', 20);
ok(!car, 'Car with id=20 should not exist');

run(function() {
store.fetchById('car', 20).then(function (car) {
equal(car.get('make'), 'BMCW', 'Car with id=20 is now loaded');
});
});
});

test("Using store#fetchById on existing record reloads it", function() {
expect(2);
var car;

Expand All @@ -220,29 +264,108 @@ test("Using store#fetch on existing record reloads it", function() {
equal(car.get('make'), 'BMC');

run(function() {
store.fetch('car', 1).then(function(car) {
store.fetchById('car', 1).then(function(car) {
equal(car.get('make'), 'BMCW');
});
});
});

test("Using store#fetch on non existing record calls find", function() {
module("integration/store - fetchAll", {
setup: function() {
initializeStore(DS.RESTAdapter.extend());
}
});

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

ajaxResponse({
cars: [{
id: 20,
id: 1,
make: 'BMC',
model: 'Mini'
},
{
id: 2,
make: 'BMCW',
model: 'Isetta'
}]
});

var cars = store.all('car');
ok(!cars.get('length'), 'There is no cars in the store');

run(function() {
store.fetchAll('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() {
expect(3);

run(function() {
store.push('car', {
id: 1,
make: 'BMC',
model: 'Mini'
});
});

ajaxResponse({
cars: [{
id: 1,
make: 'BMC',
model: 'New Mini'
},
{
id: 2,
make: 'BMCW',
model: 'Isetta'
}]
});

var car = store.hasRecordForId('car', 20);
ok(!car, 'Car with id=20 should not exist');
var cars = store.all('car');
equal(cars.get('length'), 1, 'There is one car in the store');

run(function() {
store.fetch('car', 20).then(function (car) {
equal(car.get('make'), 'BMCW', 'Car with id=20 is now loaded');
store.fetchAll('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("When is used store#fetchAll with existing, and the response doesn't contain some of those records, WHAT DO WE DO?!?!?!", function() {
expect(4);

run(function() {
store.push('car', { id: 1, make: 'BMC', model: 'Mini' });
store.push('car', { id: 2, make: 'BMCW', model: 'Isetta' });
});

ajaxResponse({
cars: [{
id: 1,
make: 'BMC',
model: 'New Mini'
}]
});

var cars = store.all('car');
equal(cars.get('length'), 2, 'There is two cars in the store');

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

var carsInStore = store.all('car');
equal(carsInStore.get('length'), 2, 'There is 2 cars in the store');
});
});

});

0 comments on commit 674678c

Please sign in to comment.