Event Aggregator
生活随笔
收集整理的這篇文章主要介紹了
Event Aggregator
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** Created with JetBrains WebStorm.* User: 宇喬* Date: 13-8-2* Time: 下午3:01* To change this template use File | Settings | File Templates.*/function Event(name) {var handlers = [];this.getName = function () {return name;}this.addHandler = function (handler) {handlers.push(handler);}this.removeHandler = function (handler) {handlers.forEach(function (item, i) {if (item == handler) {handler.splice(i, 1);}})}this.fire = function (eventArgs) {handlers.forEach(function (h) {h(eventArgs);})}
}function EventAggregator() {var events = [];function getEvent(name) {var fn;events.forEach(function (item) {if (item.getName() == name) {fn = item;return;}});return fn;}this.subscribe = function (eventName, handler) {var event = getEvent(eventName);if (!event) {event = new Event(eventName);events.push(event);}event.addHandler(handler);}this.publish = function (eventName, eventArgs) {var event = getEvent(eventName);if (!event) {event = new Event(eventName);events.push(event);}event.fire(eventArgs);}
}
轉載于:https://www.cnblogs.com/Mr-Joe/p/3232729.html
總結
以上是生活随笔為你收集整理的Event Aggregator的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有哪些下载古建筑CAD素材和SU素材的网
- 下一篇: C++中const——由一个例子想到的