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