|
| 1 | +var app = angular.module('application', ['ui.scroll']); |
| 2 | + |
| 3 | +app.factory('Server', [ |
| 4 | + '$timeout', '$q', function ($timeout, $q) { |
| 5 | + |
| 6 | + var ServerFactory = { |
| 7 | + |
| 8 | + firstIndex: 1, |
| 9 | + |
| 10 | + lastIndex: 90, |
| 11 | + |
| 12 | + delay: 100, |
| 13 | + |
| 14 | + data: [], |
| 15 | + |
| 16 | + generateId: function () { |
| 17 | + var d = '-'; |
| 18 | + function S4() { |
| 19 | + return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); |
| 20 | + } |
| 21 | + return (S4() + S4() + d + S4() + d + S4() + d + S4() + d + S4() + S4() + S4()); |
| 22 | + }, |
| 23 | + |
| 24 | + generateItem: function (number) { |
| 25 | + return { |
| 26 | + index: number, |
| 27 | + id: this.generateId(), |
| 28 | + content: 'Item #' + number |
| 29 | + } |
| 30 | + }, |
| 31 | + |
| 32 | + init: function () { |
| 33 | + for (var i = this.firstIndex; i <= this.lastIndex; i++) { |
| 34 | + this.data.push(this.generateItem(i)); |
| 35 | + } |
| 36 | + }, |
| 37 | + |
| 38 | + getItem: function (index) { |
| 39 | + for (var i = this.data.length - 1; i >= 0; i--) { |
| 40 | + if (this.data[i].index === index) { |
| 41 | + return this.data[i]; |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + |
| 46 | + returnDeferredResult: function (result) { |
| 47 | + var deferred = $q.defer(); |
| 48 | + $timeout(function () { |
| 49 | + deferred.resolve(result); |
| 50 | + }, this.delay); |
| 51 | + return deferred.promise; |
| 52 | + }, |
| 53 | + |
| 54 | + request: function (index, count) { |
| 55 | + var start = index; |
| 56 | + var end = index + count - 1; |
| 57 | + var item, result = { |
| 58 | + items: [] |
| 59 | + }; |
| 60 | + if (start <= end) { |
| 61 | + for (var i = start; i <= end; i++) { |
| 62 | + if (item = this.getItem(i)) { |
| 63 | + result.items.push(item); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return this.returnDeferredResult(result); |
| 68 | + }, |
| 69 | + |
| 70 | + prependItem: function (params) { |
| 71 | + var prependedDataIndex = this.firstIndex-- - 1; |
| 72 | + var newItem = this.generateItem(prependedDataIndex); |
| 73 | + newItem.content += params; |
| 74 | + this.data.unshift(newItem); |
| 75 | + return this.returnDeferredResult(newItem); |
| 76 | + }, |
| 77 | + |
| 78 | + appendItem: function (params) { |
| 79 | + var appendedDataIndex = this.lastIndex++ + 1; |
| 80 | + var newItem = this.generateItem(appendedDataIndex); |
| 81 | + newItem.content += params; |
| 82 | + this.data.push(newItem); |
| 83 | + return this.returnDeferredResult(newItem); |
| 84 | + }, |
| 85 | + |
| 86 | + removeItemById: function (itemId) { |
| 87 | + var length = this.data.length; |
| 88 | + for (var i = 0; i < length; i++) { |
| 89 | + if (this.data[i].id === itemId) { |
| 90 | + this.data.splice(i, 1); |
| 91 | + for (var j = i; j < length - 1; j++) { |
| 92 | + this.data[j].index--; |
| 93 | + } |
| 94 | + return this.returnDeferredResult(true); |
| 95 | + } |
| 96 | + } |
| 97 | + return this.returnDeferredResult(false); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + ServerFactory.init(); |
| 102 | + |
| 103 | + return ServerFactory; |
| 104 | + |
| 105 | + } |
| 106 | +]); |
| 107 | + |
| 108 | + |
| 109 | +app.controller('mainController', [ |
| 110 | + '$scope', 'Server', function ($scope, Server) { |
| 111 | + |
| 112 | + $scope.datasource = { |
| 113 | + get: function (index, count, success) { |
| 114 | + console.log('request by index = ' + index + ', count = ' + count); |
| 115 | + Server.request(index, count).then(function (result) { |
| 116 | + if (result.items.length) { |
| 117 | + console.log('resolved ' + result.items.length + ' items'); |
| 118 | + } |
| 119 | + success(result.items); |
| 120 | + }); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + $scope.prepend = function () { |
| 125 | + Server.prependItem(' ***').then(function (newItem) { |
| 126 | + if ($scope.adapter.isBOF()) { |
| 127 | + $scope.adapter.prepend([newItem]); |
| 128 | + } |
| 129 | + }); |
| 130 | + }; |
| 131 | + |
| 132 | + $scope.append = function () { |
| 133 | + Server.appendItem(' ***').then(function (newItem) { |
| 134 | + if ($scope.adapter.isEOF()) { |
| 135 | + $scope.adapter.append([newItem]); |
| 136 | + } |
| 137 | + }); |
| 138 | + }; |
| 139 | + |
| 140 | + /*$scope.removeAll = function () { |
| 141 | + $scope.adapter.applyUpdates(function (item) { |
| 142 | + if (item.id) { |
| 143 | + return []; |
| 144 | + } |
| 145 | + }); |
| 146 | + };*/ |
| 147 | + |
| 148 | + $scope.remove = function (itemRemove) { |
| 149 | + Server.removeItemById(itemRemove.id).then(function (result) { |
| 150 | + if (result) { |
| 151 | + $scope.adapter.applyUpdates(function (item) { |
| 152 | + if (item.id === itemRemove.id) { |
| 153 | + return []; |
| 154 | + } |
| 155 | + }); |
| 156 | + } |
| 157 | + }); |
| 158 | + }; |
| 159 | + |
| 160 | + } |
| 161 | +]); |
0 commit comments