list.page.js
2.63 KB
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
const Vue = require('yoho-vue');
const lazyload = require('yoho-vue-lazyload');
const infinitScroll = require('yoho-vue-infinite-scroll');
const bus = require('common/vue-bus');
const sort = require('product/sort.vue');
const list = require('product/list.vue');
const drawer = require('product/drawer.vue');
const filter = require('product/filter.vue');
Vue.use(lazyload);
Vue.use(infinitScroll);
require('common/vue-filter');
const app = new Vue({
el: '#product-list',
data: function() {
return {
sortConfig: global.sortConfig,
filterConfig: [],
// query
url: 'list',
sort: 3,
filter: [],
page: 0, // page= 0 未搜索, 1 并且productList.length =0 没有数据, page = page_total 全部加载完
totalPage: 0,
// 产品列表
productList: [],
// state:
inSearching: false
};
},
components: {
list,
sort,
filter,
drawer
},
methods: {
search: function() {
if (this.inSearching) {
return;
}
const self = this;
this.inSearching = true;
$.post(this.url, {
sort: this.sort,
filter: this.filter,
page: this.page
})
.done(res => {
self.$set('productList', self.productList.concat(res.data.productList));
})
.fail(error => {
self.page--;
})
.always(()=>{
self.inSearching = false;
});
},
pageState: function() {
},
/**
*
*/
research: function() {
}
},
watch: {
/* sort 和 filter 改变 都会触发 重新搜索 (想象成清空所有分页) */
sort: function() {
this.page = 0;
},
filter: function() {
this.page = 0;
},
page: function(newVal, oldVal) {
if (newVal === 0) {
// when research
this.$set('productList', []);
this.search();
} else if (newVal > oldVal && newVal > this.totalPage) {
// when fetch
this.search();
}
}
},
created: function() {
this.search();
}
});
bus.$on('list.paging', function() {
app.page++;
});
bus.$on('sort.change', function({ val}) {
console.log(val);
app.sort = val;
});
bus.$on('filter.change', function({ val}) {
console.log(val);
app.filter = val;
});