Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from skaterdav85/v1
adds DS.getAll
- Loading branch information
Showing
5 changed files
with
180 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
function errorPrefix(resourceName) { | ||
return 'DS.getAll(' + resourceName + '[, ids]): '; | ||
} | ||
|
||
/** | ||
* @doc method | ||
* @id DS.sync methods:getAll | ||
* @name getAll | ||
* @description | ||
* Synchronously return all of the resource. | ||
* | ||
* ## Signature: | ||
* ```js | ||
* DS.getAll(resourceName[, ids]) | ||
* ``` | ||
* | ||
* ## Example: | ||
* | ||
* ```js | ||
* DS.getAll('document'); // [{ author: 'John Anderson', id: 5 }] | ||
* ``` | ||
* | ||
* ## Throws | ||
* | ||
* - `{IllegalArgumentError}` | ||
* - `{NonexistentResourceError}` | ||
* | ||
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc. | ||
* @param {array} ids Optional list of primary keys to filter the array of results by. | ||
* | ||
* @returns {array} The items of the type specified by `resourceName`. | ||
*/ | ||
function getAll(resourceName, ids) { | ||
var DS = this; | ||
var IA = DS.errors.IA; | ||
var resource; | ||
var collection = []; | ||
|
||
if (!DS.definitions[resourceName]) { | ||
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName); | ||
} else if (arguments.length === 2 && !DS.utils.isArray(ids)) { | ||
throw new IA(errorPrefix(resourceName, ids) + 'ids: Must be an array!'); | ||
} | ||
|
||
resource = DS.store[resourceName]; | ||
|
||
if (DS.utils.isArray(ids)) { | ||
for (var i = 0; i < ids.length; i++) { | ||
collection.push(resource.index.get(ids[i])); | ||
} | ||
} else { | ||
collection = resource.collection.slice(); | ||
} | ||
|
||
return collection; | ||
} | ||
|
||
module.exports = getAll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
describe('DS.getAll(resourceName)', function () { | ||
function errorPrefix(resourceName) { | ||
return 'DS.getAll(' + resourceName + '[, ids]): '; | ||
} | ||
|
||
beforeEach(startInjector); | ||
|
||
it('should throw an error when method pre-conditions are not met', function () { | ||
assert.throws(function () { | ||
DS.getAll('does not exist'); | ||
}, DS.errors.NonexistentResourceError, errorPrefix('does not exist', {}) + 'does not exist is not a registered resource!'); | ||
|
||
angular.forEach(TYPES_EXCEPT_ARRAY, function (key) { | ||
assert.throws(function () { | ||
DS.getAll('post', key); | ||
}, DS.errors.IllegalArgumentError, errorPrefix('post', key) + 'ids: Must be an array!'); | ||
}); | ||
}); | ||
it('should return an array of all items in the store', function() { | ||
assert.isArray(DS.getAll('post'), 'should be an empty array'); | ||
}); | ||
it('should return results that match a set of ids', function() { | ||
DS.inject('post', [ p1, p2, p3 ]); | ||
var posts = DS.getAll('post', [ 5, 7 ]); | ||
assert.deepEqual(angular.toJson(posts), angular.toJson([ p1, p3 ])); | ||
}); | ||
}); |