-
Notifications
You must be signed in to change notification settings - Fork 107
/
append.js
124 lines (98 loc) · 2.54 KB
/
append.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var app = angular.module('application', ['ui.scroll', 'ui.scroll.jqlite']);
app.factory('Server', [
'$timeout', '$q', function ($timeout, $q) {
var ServerFactory = {
max: 50,
first: 1,
delay: 100,
data: [],
prependedData: [],
appendedData: [],
generateItem: function (number) {
return {
number: number,
content: 'Item #' + number
}
},
init: function () {
for (var i = this.first - 1; i <= this.max; i++) {
this.data.push(this.generateItem(i));
}
},
getItem: function (index) {
if (index < this.first) {
return this.prependedData[(-1) * index];
}
else if (index > this.max) {
return this.appendedData[index - this.max - 1];
}
else {
return this.data[index];
}
},
request: function (index, count) {
var self = this;
var deferred = $q.defer();
var start = index;
var end = index + count - 1;
$timeout(function () {
var item, result = {
items: []
};
if (start <= end) {
for (var i = start; i <= end; i++) {
if (item = self.getItem(i)) {
result.items.push(item);
}
}
}
deferred.resolve(result);
}, self.delay);
return deferred.promise;
},
prependItem: function (params) {
var prependedDataIndex = this.first - this.prependedData.length - 1;
var newItem = this.generateItem(prependedDataIndex);
newItem.content += params;
this.prependedData.push(newItem);
return newItem;
},
appendItem: function (params) {
var appendedDataIndex = this.max + this.appendedData.length + 1;
var newItem = this.generateItem(appendedDataIndex);
newItem.content += params;
this.appendedData.push(newItem);
return newItem;
}
};
ServerFactory.init();
return ServerFactory;
}
]);
app.controller('mainController', [
'$scope', 'Server', function ($scope, Server) {
$scope.datasource = {
get: function (index, count, success) {
console.log('request by index = ' + index + ', count = ' + count);
Server.request(index, count).then(function (result) {
if (result.items.length) {
console.log('resolved ' + result.items.length + ' items');
}
success(result.items);
});
}
};
$scope.prepend = function () {
var newItem = Server.prependItem(' (new)*');
if ($scope.adapter.isBOF()) {
$scope.adapter.prepend([newItem]);
}
};
$scope.append = function () {
var newItem = Server.appendItem(' (new)*');
if ($scope.adapter.isEOF()) {
$scope.adapter.append([newItem]);
}
};
}
]);