<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>圣诞主题</title>
<script src="observer.js"></script>
</head>
<body>
<button>点击触发page任务</button>
<button>点击取消page任务</button>
<script type="text/javascript">
//观察者
var observer = new Observer();
//page函数
//模拟运行1秒的一个函数
function page() {
setTimeout(function() {
//发布一个完成的时间
observer.publish("compelte")
}, 1000)
}
//订阅一个完成的时间
observer.subscribe("compelte", function() {
alert("page任务完成")
})
//触发任务
document.querySelectorAll("button")[0].addEventListener("click", function() {
page();
}, false)
//取消任务
document.querySelectorAll("button")[1].addEventListener("click", function() {
observer.unsubscribe("compelte")
alert("注销了订阅了compelte事件")
}, false)
</script>
</body>
</html>
/**
* 事件
* 观察者模式
*/
var Observer = (function(slice) {
function bind(event, fn) {
var events = this.events = this.events || {},
parts = event.split(/\s+/),
i = 0,
num = parts.length,
part;
if (events[event] && events[event].length) return this;
for (; i < num; i++) {
events[(part = parts[i])] = events[part] || [];
events[part].push(fn);
}
return this;
}
function one(event, fn) {
this.bind(event, function fnc() {
fn.apply(this, slice.call(arguments));
this.unbind(event, fnc);
});
return this;
}
function unbind(event, fn) {
var events = this.events,
eventName, i, parts, num;
if (!events) return;
parts = event.split(/\s+/);
for (i = 0, num = parts.length; i < num; i++) {
if ((eventName = parts[i]) in events !== false) {
events[eventName].splice(events[eventName].indexOf(fn), 1);
if (!events[eventName].length) { //修正没有事件直接删除空数组
delete events[eventName];
}
}
}
return this;
}
function trigger(event) {
var events = this.events,
i, args, falg;
if (!events || event in events === false) return;
args = slice.call(arguments, 1);
for (i = events[event].length - 1; i >= 0; i--) {
falg = events[event][i].apply(this, args);
}
return falg; //修正带返回
}
return function() {
this.on =
this.subscribe = bind;
this.off =
this.unsubscribe = unbind;
this.trigger =
this.publish = trigger;
this.one = one;
return this;
};
})([].slice);