Skip to content
This repository was archived by the owner on Apr 2, 2019. It is now read-only.

Commit 834515d

Browse files
committed
Infinite paging, first pass WIP
1 parent 24c701a commit 834515d

File tree

6 files changed

+227
-11
lines changed

6 files changed

+227
-11
lines changed

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ module.exports = function (grunt) {
6363
"src/header.js",
6464
"src/body.js",
6565
"src/footer.js",
66-
"src/grid.js"
66+
"src/grid.js",
67+
"src/container.js"
6768
],
6869
dest: "lib/backgrid.js"
6970
}

lib/backgrid.js

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
backgrid
33
http://github.com/wyuenho/backgrid
44
5-
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors <[email protected]>
5+
Copyright (c) 2014 Jimmy Yuen Ho Wong and contributors <[email protected]>
66
Licensed under the MIT license.
77
*/
88

@@ -2872,5 +2872,79 @@ var Grid = Backgrid.Grid = Backbone.View.extend({
28722872
}
28732873

28742874
});
2875-
return Backgrid;
2875+
2876+
/*
2877+
backgrid
2878+
http://github.com/wyuenho/backgrid
2879+
2880+
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
2881+
Licensed under the MIT license.
2882+
*/
2883+
2884+
var Container = Backgrid.Container = Backbone.View.extend({
2885+
events: {
2886+
"scroll": "onScroll"
2887+
},
2888+
2889+
/** @property */
2890+
prefetch: 1,
2891+
2892+
/** @property */
2893+
throttle: 200,
2894+
2895+
/** @property */
2896+
tagName: "div",
2897+
2898+
/** @property */
2899+
className: "backgrid-container",
2900+
2901+
initialize: function (options) {
2902+
if (!options.grid) throw "grid is a mandatory options for Backgrid.Container";
2903+
2904+
this.height = options.height || 200;
2905+
this.throttle = options.throttle || this.throttle;
2906+
this.grid = options.grid;
2907+
this.onScroll = _.debounce(_.bind(this.onScroll, this), this.throttle);
2908+
this.indicator = $("<h1>HEY I'M LOADING OVER HERE</h1>").hide();
2909+
2910+
this.listenTo(this.grid.collection.pageableCollection, "request", this.onRequest);
2911+
},
2912+
2913+
onScroll: function (e) {
2914+
var top = e.target.scrollTop,
2915+
prefetch = this.prefetch,
2916+
maxScrollTop = e.target.scrollHeight - this.$el.height(),
2917+
collection = this.grid.collection.pageableCollection,
2918+
indicator = this.indicator,
2919+
next;
2920+
2921+
if (top >= maxScrollTop) {
2922+
if (next = collection.getNextPage({
2923+
scrolling: true
2924+
})) {
2925+
next.then(function() {
2926+
prefetch && collection.hasNext() && collection.getNextPage({
2927+
scrolling: true
2928+
});
2929+
}).always(function() {
2930+
indicator.hide();
2931+
});
2932+
}
2933+
}
2934+
},
2935+
2936+
onRequest: function (collection, xhr, options) {
2937+
if (options.scrolling) this.indicator.show();
2938+
},
2939+
2940+
render: function () {
2941+
this.grid.render().$el.appendTo(this.$el.css({
2942+
height: this.height + "px",
2943+
overflow: "scroll"
2944+
}));
2945+
this.indicator.detach().appendTo(this.grid.footer.el);
2946+
this.delegateEvents();
2947+
return this;
2948+
}
2949+
});return Backgrid;
28762950
}));

lib/backgrid.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/container.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
backgrid
3+
http://github.com/wyuenho/backgrid
4+
5+
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
6+
Licensed under the MIT license.
7+
*/
8+
9+
var Container = Backgrid.Container = Backbone.View.extend({
10+
events: {
11+
"scroll": "onScroll"
12+
},
13+
14+
/** @property */
15+
prefetch: 1,
16+
17+
/** @property */
18+
throttle: 200,
19+
20+
/** @property */
21+
tagName: "div",
22+
23+
/** @property */
24+
className: "backgrid-container",
25+
26+
initialize: function (options) {
27+
if (!options.grid) throw "grid is a mandatory options for Backgrid.Container";
28+
29+
this.height = options.height || 200;
30+
this.throttle = options.throttle || this.throttle;
31+
this.grid = options.grid;
32+
this.onScroll = _.debounce(_.bind(this.onScroll, this), this.throttle);
33+
this.indicator = $("<h1>HEY I'M LOADING OVER HERE</h1>").hide();
34+
35+
this.listenTo(this.grid.collection.pageableCollection, "request", this.onRequest);
36+
},
37+
38+
onScroll: function (e) {
39+
var top = e.target.scrollTop,
40+
prefetch = this.prefetch,
41+
maxScrollTop = e.target.scrollHeight - this.$el.height(),
42+
collection = this.grid.collection.pageableCollection,
43+
indicator = this.indicator,
44+
next;
45+
46+
if (top >= maxScrollTop) {
47+
if (next = collection.getNextPage({
48+
scrolling: true
49+
})) {
50+
next.then(function() {
51+
prefetch && collection.hasNext() && collection.getNextPage({
52+
scrolling: true
53+
});
54+
}).always(function() {
55+
indicator.hide();
56+
});
57+
}
58+
}
59+
},
60+
61+
onRequest: function (collection, xhr, options) {
62+
if (options.scrolling) this.indicator.show();
63+
},
64+
65+
render: function () {
66+
this.grid.render().$el.appendTo(this.$el.css({
67+
height: this.height + "px",
68+
overflow: "scroll"
69+
}));
70+
this.indicator.detach().appendTo(this.grid.footer.el);
71+
this.delegateEvents();
72+
return this;
73+
}
74+
});

test/container.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
backgrid
3+
http://github.com/wyuenho/backgrid
4+
5+
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
6+
Licensed under the MIT license.
7+
*/
8+
describe("A Container", function () {
9+
var Book = Backbone.Model.extend({});
10+
var Books = Backbone.Collection.extend({
11+
model: Book
12+
});
13+
14+
var books;
15+
var grid;
16+
var container;
17+
18+
beforeEach(function () {
19+
books = new Books([{
20+
id: 1,
21+
title: "Alice's Adventures in Wonderland"
22+
}, {
23+
id: 2,
24+
title: "A Tale of Two Cities"
25+
}, {
26+
id: 3,
27+
title: "The Catcher in the Rye"
28+
}]);
29+
30+
grid = new Backgrid.Grid({
31+
columns: [{
32+
name: "title",
33+
cell: "string"
34+
}],
35+
collection: books,
36+
footer: Backgrid.Footer
37+
});
38+
39+
container = new Backgrid.Container({
40+
grid: grid
41+
});
42+
});
43+
44+
it("throws if the grid isn't passed as an option", function () {
45+
expect(function(){
46+
new Backgrid.Container({});
47+
}).toThrow("grid is a mandatory options for Backgrid.Container");
48+
});
49+
50+
it("renders the grid within it when rendered", function () {
51+
spyOn(grid, "trigger");
52+
spyOn(grid.header, "render").andCallThrough();
53+
spyOn(grid.footer, "render").andCallThrough();
54+
spyOn(grid.body, "render").andCallThrough();
55+
56+
container.render();
57+
58+
expect(grid.el.tagName).toBe("TABLE");
59+
expect(grid.header.render.calls.length).toBe(1);
60+
expect(grid.footer.render.calls.length).toBe(1);
61+
expect(grid.body.render.calls.length).toBe(1);
62+
expect(grid.trigger.calls.length).toBe(1);
63+
expect(grid.trigger).toHaveBeenCalledWith("backgrid:rendered", grid);
64+
});
65+
66+
});

test/index.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
<head>
44
<meta charset="utf-8">
55
<title>Backgrid.js Jasmine Spec Runner</title>
6-
6+
77
<link rel="stylesheet" type="text/css" href="vendor/css/jasmine.css">
88
<script src="vendor/js/jasmine.js"></script>
99
<script src="vendor/js/jasmine-html.js"></script>
10-
10+
1111
<script src="vendor/js/jquery.js"></script>
1212
<script src="vendor/js/underscore.js"></script>
1313
<script src="vendor/js/backbone.js"></script>
1414
<script src="vendor/js/backbone-pageable.js"></script>
1515
<script src="../lib/backgrid.js"></script>
16-
16+
1717
<script src="preamble.js"></script>
1818
<script src="column.js"></script>
1919
<script src="formatter.js"></script>
@@ -22,7 +22,8 @@
2222
<script src="body.js"></script>
2323
<script src="header.js"></script>
2424
<script src="grid.js"></script>
25-
25+
<script src="container.js"></script>
26+
2627
<script>
2728
(function () {
2829
var jasmineEnv = jasmine.getEnv();
@@ -41,9 +42,9 @@
4142
});
4243
}());
4344
</script>
44-
45+
4546
</head>
46-
47+
4748
<body>
4849
</body>
4950
</html>

0 commit comments

Comments
 (0)