中介

中介
var mediator = (function () {
    var topics = {};

    var subscribe = function (topic, fn) {
        if(!topics[topic]) {
            topics[topic] = [];
        }
        
        topics[topic].push({
            context : this,
            callback : fn
        });

        return this;
    }

    var publish = function (topic) {
        var args;
        if(!topics[topic]) {
            return false;
        }

        args = Array.prototype.slice.call(arguments, 1);
        for(var i = 0, l = topics[topic].length; i < l; i++) {
            var subscription = topics[topic][i];
            subscription.callback.apply(subscription.context, args);
        }
        return this;
    }

    return {
        Publicsh : publish,
        Subscribe : subscribe,
        installTo : function (obj) {
            obj.subscribe = subscribe;
            obj.publish = publish;
        }
    }
}());
View Code

 

上一篇:Docker安装Gitlab


下一篇:Redis实现消息的发布和订阅