controller.js
3.54 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
import {
Controller
} from 'js/yoho-mvc';
import {
NavView,
ContentView
} from './view';
import {
globalSearch as search
} from '../models';
let filter = require('js/plugin/filter');
let noResultHbs = require('hbs/product/search/no-result-new.hbs');
class ListController extends Controller {
constructor() {
super();
this.navView = new NavView();
this.content = new ContentView();
this.page = 1;
this.searching = false;
this.nav = this.navView.getDefaultNav();
this.nav.reload = false;
this.query = $.extend(this.queryString(), {
type: 'new',
order: this.nav.order
});
this.navView.on('nav-change', this.doNavChange.bind(this));
this.content.on('search', this.doSearch.bind(this));
this.created();
}
queryString() {
let vars = {},
hash,
i;
let hashes = window.location.search.slice(1).split('&');
for (i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
}
return vars;
}
doNavChange(e, nav, navType) {
this.content.doContentChange(e, navType);
this.nav = nav;
if (nav.reload) {
this.nav.page = 0;
} else if (nav.end) {
return;
}
this.query = $.extend(this.query, {
type: navType,
order: nav.order
});
if (this.nav.page === 0) {
this.search();
}
}
doSearch() {
if (!this.nav.end) {
this.search();
}
}
search(opt) {
if (this.searching) {
return;
}
let ext = {};
if (opt) {
switch (opt.type) {
case 'ageLevel':
ext = {
age_level: opt.id
};
break;
default:
ext[opt.type] = opt.id;
break;
}
this.query = $.extend({}, this.query, ext);
this.nav.reload = true;
this.nav.page = 0;
['sort', 'color', 'price'].forEach((k) => {
if (this.query[k] === 0) {
delete this.query[k];
}
});
}
let page = this.nav.page + 1;
let params = $.extend({}, this.query, {
page: page,
currentUrl: location.pathname
});
this.searching = true;
search('//m.yohobuy.com/product/global/search', params).then(data => {
this.nav.page = page;
if (!data) {
this.nav.end = true;
if (this.nav.reload) {
this.content.setList(noResultHbs(), {
reload: true
});
}
} else {
if (this.nav.reload) {
this.content.setList(data, {
reload: true
});
} else {
this.content.setList(data, {});
}
}
}).catch(() => {}).finally(() => {
this.nav.reload = false;
this.searching = false;
});
}
created() {
this.navView.setFilter(filter);
filter.initFilter({
fCbFn: this.search.bind(this),
hCbFn: () => {
this.navView.preActive();
}
});
}
}
module.exports = ListController;