-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): adds the List type and namespace.
Also adds the `List.$asList` method to generate lists from collections and other lists. Closes #169
- Loading branch information
Showing
5 changed files
with
171 additions
and
12 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
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,39 @@ | ||
'use strict'; | ||
|
||
RMModule.factory('RMListApi', [function() { | ||
|
||
/** | ||
* @class ListApi | ||
* | ||
* @description Common methods for Lists and Collections. | ||
*/ | ||
return { | ||
|
||
/** | ||
* @memberof ListApi# | ||
* | ||
* @description Generates a new list from this one. | ||
* | ||
* If called without arguments, the list is popupated with the same contents as this list. | ||
* | ||
* If there is a pending async operation on the host collection/list, then this method will | ||
* return an empty list and fill it when the async operation finishes. If you don't need the async behavior | ||
* then use `$type.list` directly to generate a new list. | ||
* | ||
* @param {function} _filter A filter function that should return the list contents as an array. | ||
* @return {ListApi} list | ||
*/ | ||
$asList: function(_filter) { | ||
var list = this.$type.list(), | ||
promise = this.$asPromise(); | ||
|
||
// set the list initial promise to the resolution of the parent promise. | ||
list.$promise = promise.then(function(_this) { | ||
list.push.apply(list, _filter ? _filter(_this) : _this); | ||
}); | ||
|
||
return list; | ||
} | ||
}; | ||
|
||
}]); |
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
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,45 @@ | ||
'use strict'; | ||
|
||
describe('Restmod list:', function() { | ||
|
||
var restmod, $httpBackend, Bike; | ||
|
||
beforeEach(module('restmod')); | ||
|
||
beforeEach(inject(function($injector) { | ||
restmod = $injector.get('restmod'); | ||
$httpBackend = $injector.get('$httpBackend'); | ||
Bike = restmod.model('/api/bikes'); | ||
})); | ||
|
||
describe('$asList', function() { | ||
|
||
it('should retrieve a list with same contents as collection if called with no arguments', function() { | ||
var query = Bike.$collection().$decode([ { model: 'Slash' }, { model: 'Remedy' } ]); | ||
var list = query.$asList().$asList().$asList(); | ||
expect(list.length).toEqual(2); | ||
expect(list[0]).toBe(query[0]); | ||
expect(list[1]).toBe(query[1]); | ||
}); | ||
|
||
it('should wait for last aync operation before populating list', function() { | ||
$httpBackend.when('GET', '/api/bikes').respond([ { model: 'Slash' }, { model: 'Remedy' } ]); | ||
var list = Bike.$search().$asList().$asList(); | ||
expect(list.length).toEqual(0); | ||
$httpBackend.flush(); | ||
expect(list.length).toEqual(2); | ||
}); | ||
|
||
it('should accept a transformation function as parameter', function() { | ||
$httpBackend.when('GET', '/api/bikes').respond([ { model: 'Slash' }, { model: 'Remedy' } ]); | ||
var list = Bike.$search().$asList(function(_original) { | ||
return [_original[1]]; | ||
}).$asList(); | ||
expect(list.length).toEqual(0); | ||
$httpBackend.flush(); | ||
expect(list.length).toEqual(1); | ||
}); | ||
|
||
}); | ||
}); | ||
|