Skip to content

Commit 7b4b674

Browse files
committed
new format for the dataset.get method
1 parent 54b7658 commit 7b4b674

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

demo/examples/scopeDatasource.coffee

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ angular.module('application', ['ui.scroll', 'ui.scroll.jqlite']).controller('mai
55

66
datasource = {}
77

8-
datasource.get = (index, count, success)->
8+
datasource.get = (descriptor, success)->
99
$timeout(
1010
->
11+
index = descriptor.index
12+
count = descriptor.count
1113
result = []
1214
for i in [index..index + count-1]
1315
result.push "item ##{i}"

src/ui-scroll.coffee

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,27 @@ angular.module('ui.scroll', [])
506506
else
507507
fetch(rid)
508508

509+
if datasource.get.length == 2
510+
fetchNext = (success) -> datasource.get
511+
index: buffer.next
512+
append: if buffer.length then buffer[buffer.length-1].item else undefined
513+
count: bufferSize
514+
success
515+
fetchPrevious = (success) -> datasource.get
516+
index: buffer.first-bufferSize
517+
prepend: if buffer.length then buffer[0].item else undefined
518+
count: bufferSize
519+
success
520+
else
521+
fetchNext = (success) -> datasource.get buffer.next, bufferSize, success
522+
fetchPrevious = (success) -> datasource.get buffer.first-bufferSize, bufferSize, success
523+
509524
fetch = (rid) ->
510525
if pending[0] # scrolling down
511526
if buffer.length && !viewport.shouldLoadBottom()
512527
adjustBufferAfterFetch rid
513528
else
514-
datasource.get buffer.next, bufferSize,
515-
(result) ->
529+
fetchNext (result) ->
516530
return if (rid and rid isnt ridActual) or $scope.$$destroyed
517531
if result.length < bufferSize
518532
buffer.eof = true
@@ -526,8 +540,7 @@ angular.module('ui.scroll', [])
526540
if buffer.length && !viewport.shouldLoadTop()
527541
adjustBufferAfterFetch rid
528542
else
529-
datasource.get buffer.first-bufferSize, bufferSize,
530-
(result) ->
543+
fetchPrevious (result) ->
531544
return if (rid and rid isnt ridActual) or $scope.$$destroyed
532545
if result.length < bufferSize
533546
buffer.bof = true

test/BasicSetupSpec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,38 @@ describe('uiScroll', function () {
6060
runTest(scrollSettings,
6161
function () {
6262
expect(spy.calls.all().length).toBe(2);
63+
expect(spy.calls.all()[0].args.length).toBe(3);
6364
expect(spy.calls.all()[0].args[0]).toBe(1);
65+
expect(spy.calls.all()[0].args[1]).toBe(10);
66+
expect(spy.calls.all()[1].args.length).toBe(3);
6467
expect(spy.calls.all()[1].args[0]).toBe(-9);
68+
expect(spy.calls.all()[1].args[1]).toBe(10);
6569
}
6670
);
6771
});
6872
});
6973

74+
describe('basic setup (new datasource get signature)', function () {
75+
var scrollSettings = {datasource: 'myNewEmptyDatasource'};
76+
77+
return;
78+
// this does not work because spy always return 0 for the number of parameters (spy.length)
79+
it('should call get on the datasource 2 times ', function () {
80+
var spy;
81+
inject(function (myNewEmptyDatasource) {
82+
spy = spyOn(myNewEmptyDatasource, 'get').and.callThrough();
83+
});
84+
runTest(scrollSettings,
85+
function () {
86+
expect(spy.calls.all().length).toBe(2);
87+
expect(spy.calls.all()[0].args.length).toBe(2);
88+
expect(spy.calls.all()[0].args[0].index).toBe(1);
89+
expect(spy.calls.all()[0].args[0].count).toBe(10);
90+
expect(spy.calls.all()[0].args.length).toBe(2);
91+
expect(spy.calls.all()[1].args[0]).index.toBe(-9);
92+
expect(spy.calls.all()[1].args[0].count).toBe(10);
93+
}
94+
);
95+
});
96+
});
7097
});

test/BasicTestsSpec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ describe('uiScroll', function () {
161161
it('should clip 3 divs from the top and add 3 more divs to the bottom (9 divs total) (+ 2 padding divs)', function () {
162162
runTest(scrollSettings,
163163
function (viewport, scope, $timeout) {
164-
// padding calculations are different now, besides the specific number of elements clipped
165-
// is not that important and a test for cliptop/bottom methods of the viewport object is more appropriate
166164
viewport.scrollTop(100);
167165
viewport.trigger('scroll');
168166
$timeout.flush();

test/datasources.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ angular.module('ui.scroll.test.datasources', [])
1010
}
1111
])
1212

13+
.factory('myNewEmptyDatasource', [
14+
'$log', '$timeout', '$rootScope', function () {
15+
return {
16+
get: function (descriptor, success) {
17+
success([]);
18+
}
19+
};
20+
}
21+
])
22+
1323
.factory('myOnePageDatasource', [
1424
'$log', '$timeout', '$rootScope', function () {
1525
return {

0 commit comments

Comments
 (0)