Skip to content

Commit df0eb53

Browse files
committed
Cursus dag vuejsdevelopers#2
1 parent 86f9b2a commit df0eb53

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

index.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
<link rel="stylesheet" type="text/css" href="/public/style.css">
1212
</head>
1313
<body>
14-
<div id="app">
14+
<div id="app" v-cloak>
15+
16+
<div id="loading">Loading...</div>
17+
1518
<div class="header">
1619
<div class="container">
1720
<div class="title">
@@ -31,7 +34,7 @@ <h1>Vue.js Poster Shop</h1>
3134
Loading...
3235
</div>
3336
<div class="search-results" v-else>
34-
Found {{ products.length }} results for search term <em>{{ lastSearch }}</em>.
37+
Found {{ results.length }} results for search term <em>{{ lastSearch }}</em>.
3538
</div>
3639
<div v-for="product in products" class="product" v-bind:key="product.id">
3740
<div>
@@ -45,7 +48,9 @@ <h4 class="product-title">{{ product.title }}</h4>
4548
<button v-on:click="addToCart(product)" class="add-to-cart btn">Add to cart</button>
4649
</div>
4750
</div>
48-
<div id="product-list-bottom"></div>
51+
<div id="product-list-bottom">
52+
<div v-if="products.length === results.length && results.length > 0">No More items.</div>
53+
</div>
4954
</div>
5055
<div class="cart">
5156
<h2>Shopping Cart</h2>

public/script.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
console.log("It works!");
2-
31
var LOAD_NUM = 4;
2+
var watcher;
43

54
new Vue({
65
el: "#app",
76
data: {
87
total: 0,
98
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!
1311
],
1412
cart: [],
1513
search: "cat",
1614
lastSearch: "",
1715
loading: false,
1816
results: [],
1917
},
18+
19+
// Functions
2020
methods: {
2121
addToCart: function(product){
2222

2323
// 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);
2628

2729
// Real code
2830
this.total += product.price;
@@ -49,11 +51,14 @@ new Vue({
4951
},
5052

5153
inc: function(item){
54+
console.log('Current item: ');
55+
console.log(item);
5256
item.qty++;
5357
this.total += item.price;
5458
},
5559

5660
dec: function(item){
61+
console.log('Current item: ');
5762
console.log(item);
5863
item.qty--;
5964
this.total -= item.price;
@@ -66,27 +71,59 @@ new Vue({
6671

6772
onSubmit: function() {
6873
this.products = [];
74+
this.results = [];
6975
this.loading = true;
70-
7176
var path = "/search?q=".concat(this.search); // search from this element (app)
7277
this.$http.get(path)
7378
.then(function(response) {
79+
console.log('Response from search: ');
7480
console.log(response);
7581
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
7783
this.lastSearch = this.search;
84+
this.appendResults();
7885
this.loading = false;
7986
});
80-
}
87+
},
8188

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+
}
82101
},
102+
103+
104+
// Filters
83105
filters: {
84106
currency: function(price) {
85107
return "$".concat(price.toFixed(2));
86108
}
87109
},
110+
111+
112+
// Livecycle hooks
88113
created: function(){
89114
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+
90127
}
91128

92-
});
129+
});

public/style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,18 @@ body {
378378

379379
.fade-enter-to {
380380
/* opacity: 1; */
381+
}
382+
383+
384+
[v-cloak] > * {
385+
display: none;
386+
}
387+
388+
[v-cloak] > #loading {
389+
display: block;
390+
padding: 1em;
391+
}
392+
393+
#loading {
394+
display: none;
381395
}

0 commit comments

Comments
 (0)