Skip to content

Commit 14b6bfb

Browse files
committed
remote emulation demo
1 parent 06d4ef3 commit 14b6bfb

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

demo/remote/remote.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Remote server emulation</title>
7+
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
8+
<script src="../../dist/ui-scroll.js"></script>
9+
<script src="remote.js"></script>
10+
<link rel="stylesheet" href="../css/style.css" type="text/css" />
11+
</head>
12+
13+
<body ng-controller="mainController" ng-app="application">
14+
<div class="cont cont-global">
15+
<a class="back" href="../index.html">browse other examples</a>
16+
<h1 class="page-header page-header-exapmle">Remote server emulation</h1>
17+
<div class="description">
18+
This demo implements Remote server emulation factory, so the datasource get method looks like
19+
<div class="code">
20+
<pre>
21+
datasource.get = function(index, count, success) {
22+
Server.request(index, count).then(success);
23+
};</pre>
24+
</div>
25+
</div>
26+
<div class="viewport" ui-scroll-viewport>
27+
<div class="item" ui-scroll="item in datasource" start-index="firstIndex">
28+
{{item.content}}
29+
</div>
30+
</div>
31+
</div>
32+
</body>
33+
34+
</html>

demo/remote/remote.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
angular.module('application', ['ui.scroll'])
2+
3+
.factory('Server', function($timeout, $q) {
4+
return {
5+
6+
default: {
7+
first: 0,
8+
max: 99,
9+
delay: 100
10+
},
11+
12+
data: [],
13+
14+
init: function(settings = {}) {
15+
this.first = settings.hasOwnProperty('first') ? settings.first : this.default.first;
16+
this.max = settings.hasOwnProperty('max') ? settings.max : this.default.max;
17+
this.delay = settings.hasOwnProperty('delay') ? settings.delay : this.default.delay;
18+
for (var i = this.first; i <= this.max; i++) {
19+
this.data[i] = {
20+
index: i,
21+
content: 'Item #' + i
22+
};
23+
}
24+
},
25+
26+
getFirst: function() {
27+
return this.first;
28+
},
29+
30+
request: function(index, count) {
31+
var self = this;
32+
var deferred = $q.defer();
33+
34+
var start = index;
35+
var end = index + count - 1;
36+
37+
$timeout(function() {
38+
var item, result = [];
39+
if (start <= end) {
40+
for (var i = start; i <= end; i++) {
41+
if (item = self.data[i]) {
42+
result.push(item);
43+
}
44+
}
45+
}
46+
deferred.resolve(result);
47+
}, self.delay);
48+
49+
return deferred.promise;
50+
}
51+
};
52+
})
53+
54+
.controller('mainController', function($scope, Server) {
55+
56+
$scope.firstIndex = 1;
57+
58+
Server.init({
59+
first: $scope.firstIndex,
60+
max: 100,
61+
delay: 40
62+
});
63+
64+
$scope.datasource = {
65+
get: function(index, count, success) {
66+
console.log('requested index = ' + index + ', count = ' + count);
67+
Server.request(index, count).then(function(result) {
68+
console.log('resolved ' + result.length + ' items');
69+
success(result);
70+
});
71+
}
72+
};
73+
});

0 commit comments

Comments
 (0)