Skip to content

Commit afb774b

Browse files
committed
添加路由对于target.href属性的判断
1 parent 2b55622 commit afb774b

File tree

9 files changed

+54
-18
lines changed

9 files changed

+54
-18
lines changed

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/*.js
2+
webpack.*.js
3+
dist/**/*.js

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "demo.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"dev": "webpack-dev-server --devtool eval-source-map --progress --colors --hot --inline --content-base ./dist"
8+
"dev": "webpack-dashboard -- webpack-dev-server --devtool eval-source-map --progress --colors --hot --inline --content-base ./dist"
99
},
1010
"repository": {
1111
"type": "git",

src/components/time-select/index.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ export default class timeSelectComponent {
7171
}
7272
initAlphaModule() {
7373
let itemArr = [],
74-
element = document.querySelector(this.configMap.containerArr[0]);
74+
element1 = document.querySelector(this.configMap.containerArr[0]),
75+
element2 = document.querySelector(this.configMap.containerArr[1]);
7576
for(let i = 65; i < 90; i++) {
7677
let letter = String.fromCharCode(i);
7778
itemArr.push({text: letter, value: letter});
@@ -82,9 +83,22 @@ export default class timeSelectComponent {
8283
title: '请选择车牌'
8384
});
8485

85-
element.addEventListener('click', function() {
86+
element1.addEventListener('click', function() {
8687
picker.show();
87-
})
88+
});
89+
90+
element2.addEventListener('click', function() {
91+
picker.show();
92+
})
93+
94+
95+
picker.on('picker.change', (index, selectIndex) => {
96+
picker.refill([{text: 1, value: 1}, {text: 2, value: 2}], 2);
97+
})
98+
99+
picker.on('picker.select', (selectedVal, selectIndex) => {
100+
101+
})
88102
}
89103
initTimeModule() {
90104
var startYearArr = this.configMap.startYearArr,

src/lib/js/controller.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@ export class Controller {
5656

5757
}
5858

59-
//页面初始化
59+
//页面初始化(willAppear阶段)
6060
getViewInit(fn) {
6161
this.viewInit = fn.bind(this) || function() {};
62+
return this;
63+
}
64+
65+
//页面销毁阶段(willDisappear阶段)
66+
getViewDestory(fn) {
67+
this.viewDestory = fn.bind(this) || function() {};
68+
return this;
6269
}
6370

6471
//获取controller的初始化状态

src/lib/js/xRoute.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const addRoute = (path = '', cb = () => { }, config = {}, context) => {
5757
const handleRoute = (path, isFromHistory) => {
5858

5959
let curContext;
60+
console.log(location.hash);
61+
6062
for (let i = 0; i < Router.length; i++) {
6163
let routeItem = Router[i];
6264
if (routeItem.path === path) {
@@ -82,23 +84,27 @@ const handleRoute = (path, isFromHistory) => {
8284
//TODO 事件冒泡路由拦截 <a href="a.html"> <a href="#/a"> 这2种写法处理起来有什么区别?
8385
//路由的写法统一为: <a data-href="aaa"></a>
8486
document.addEventListener('click', (e) => {
85-
let dataset = e.target.dataset,
87+
let href = e.target.dataset.href,
8688
oldHash = location.hash.slice(2);
87-
89+
90+
if (href) {
91+
8892
//添加钩子 路由进行跳转时模型model上数据的处理
89-
if (dataset.href === oldHash) return;
90-
if (dataset) {
91-
if (handleRoute(dataset.href)) {
93+
if (href === oldHash) return;
94+
95+
96+
if (handleRoute(href)) {
9297
//阻止默认事件
9398
e.preventDefault();
9499

95100
//通过class进行样式处理
96101
routeClassHandle(e);
102+
97103
}
98104
}
99105
});
100106

101-
107+
//路由激活状态class控制
102108
const routeClassHandle = (e) => {
103109
document.querySelector('.route-active') && document.querySelector('.route-active').classList.remove('route-active');
104110
e.target.classList.add('route-active');

src/modules/pageA/a-controller.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ controller
4545

4646
timeSelect.configModule({
4747
startYearArr: [1972],
48-
containerArr: ['#name'],
48+
containerArr: ['#name', '#name-test'],
4949
callbackArr: [function (time) {
5050
console.log(time);
5151
}]
@@ -106,8 +106,13 @@ controller
106106
});
107107

108108

109+
})
110+
.getViewDestory(function() {
111+
console.log('PageA is leaving now');
109112
});
110113

114+
115+
111116
export {
112117
controller
113118
}

src/modules/pageA/a.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
</div>
77

88

9-
<button id="name">点击我</button>
9+
<button id="name">name</button>
10+
<button id="name-test">name test</button>
1011
<button id="age">点我</button>
1112

1213

14+
1315
<div class="btn" style="margin-top: 50px;">
1416
点击显示城市选择
1517
</div>

src/route.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import {modelB} from './modules/pageB/b-model';*/
55
import {controller} from './modules/pageA/a-controller';
66

77

8-
Router.addRoute('aaa', () => {
8+
Router.addRoute('aaa', function () {
99
//modelA.pageInit();
1010
let page = document.querySelector('.page-container:first-child');
1111
if (!controller.getInitedStatus) {
1212
page.innerHTML = require('modules/pageA/a.html');
1313
controller.init();
1414
}
15-
16-
},{cache: 'on'});
15+
},{cache: 'on'}, controller.viewDestory);
1716

1817
Router.addRoute('bbb', () => {
1918
//modelB.pageInit();

webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ module.exports ={
1010
//生成source-map文件
1111
devtool: 'eval-source-map',
1212
module: {
13-
preLoaders: [
13+
/*preLoaders: [
1414
{
1515
test: /\.js$/,
1616
exclude: /node_modules|picker.min.js$|dialog.js$|imgResize.js$/,
1717
loader: 'eslint'
1818
}
19-
],
19+
],*/
2020
loaders: [
2121
{
2222
test: /\.js$/,

0 commit comments

Comments
 (0)