1
- console . log ( "It works!" ) ;
2
-
3
1
var LOAD_NUM = 4 ;
2
+ var watcher ;
4
3
5
4
new Vue ( {
6
5
el : "#app" ,
7
6
data : {
8
7
total : 0 ,
9
8
products : [
10
- // { title: "product 1", id: 1, price: 9.99 },
11
- // { title: "product 2", id: 2, price: 9.99 },
12
- // { title: "product 3", id: 3, price: 9.99 }
9
+ // { title: "product 1", id: 1, price: 9.99 }
10
+ // Is now dynamic from search request!
13
11
] ,
14
12
cart : [ ] ,
15
13
search : "cat" ,
16
14
lastSearch : "" ,
17
15
loading : false ,
18
16
results : [ ] ,
19
17
} ,
18
+
19
+ // Functions
20
20
methods : {
21
21
addToCart : function ( product ) {
22
22
23
23
// Debug
24
- console . log ( product ) ; // current object
25
- console . log ( this . total ) ; // total from this element (app)
24
+ console . log ( 'Current product object: ' ) ;
25
+ console . log ( product ) ;
26
+ console . log ( 'Total price from element(#app): ' ) ;
27
+ console . log ( this . total ) ;
26
28
27
29
// Real code
28
30
this . total += product . price ;
@@ -49,11 +51,14 @@ new Vue({
49
51
} ,
50
52
51
53
inc : function ( item ) {
54
+ console . log ( 'Current item: ' ) ;
55
+ console . log ( item ) ;
52
56
item . qty ++ ;
53
57
this . total += item . price ;
54
58
} ,
55
59
56
60
dec : function ( item ) {
61
+ console . log ( 'Current item: ' ) ;
57
62
console . log ( item ) ;
58
63
item . qty -- ;
59
64
this . total -= item . price ;
@@ -66,27 +71,59 @@ new Vue({
66
71
67
72
onSubmit : function ( ) {
68
73
this . products = [ ] ;
74
+ this . results = [ ] ;
69
75
this . loading = true ;
70
-
71
76
var path = "/search?q=" . concat ( this . search ) ; // search from this element (app)
72
77
this . $http . get ( path )
73
78
. then ( function ( response ) {
79
+ console . log ( 'Response from search: ' ) ;
74
80
console . log ( response ) ;
75
81
this . results = response . body ;
76
- this . products = response . body . slice ( 0 , LOAD_NUM ) ; // fill product array with search results
82
+ // this.products = response.body.slice(0, LOAD_NUM); // fill product array with search results
77
83
this . lastSearch = this . search ;
84
+ this . appendResults ( ) ;
78
85
this . loading = false ;
79
86
} ) ;
80
- }
87
+ } ,
81
88
89
+ appendResults : function ( ) {
90
+ console . log ( 'Append results:' ) ;
91
+ console . log ( this . products ) ;
92
+ console . log ( this . results ) ;
93
+ if ( this . products . length < this . results . length ) {
94
+ var toAppend = this . results . slice (
95
+ this . products . length ,
96
+ LOAD_NUM + this . products . length
97
+ ) ;
98
+ this . products = this . products . concat ( toAppend ) ;
99
+ }
100
+ }
82
101
} ,
102
+
103
+
104
+ // Filters
83
105
filters : {
84
106
currency : function ( price ) {
85
107
return "$" . concat ( price . toFixed ( 2 ) ) ;
86
108
}
87
109
} ,
110
+
111
+
112
+ // Livecycle hooks
88
113
created : function ( ) {
89
114
this . onSubmit ( ) ;
115
+ } ,
116
+ updated : function ( ) {
117
+ var sensor = document . querySelector ( '#product-list-bottom' ) ;
118
+ watcher = scrollMonitor . create ( sensor ) ;
119
+ watcher . enterViewport ( this . appendResults ) ;
120
+ } ,
121
+ beforeUpdate : function ( ) {
122
+ if ( watcher ) {
123
+ watcher . destroy ( ) ;
124
+ watchter = null ;
125
+ }
126
+
90
127
}
91
128
92
- } ) ;
129
+ } ) ;
0 commit comments