pub/sub模式的jquery插件

来源:互联网 发布:mysql 外网访问 编辑:程序博客网 时间:2024/06/04 19:38
//------------------------------// pub sub plugin//------------------------------;(function(d){    var cache = {};    d.publish = function(/* String */topic, /* Array? */args){        try{            d.each(cache[topic], function(){                console.log(cache[topic]);                this.apply(d, args || []);            });        } catch (err) {            // handle this error            console.log(err);        }    };    d.subscribe = function(/* String */topic, /* Function */callback){        if(!cache[topic]){            cache[topic] = [];        }        cache[topic].push(callback);        //return [topic, callback]; // Array    };    d.unsubscribe = function(/* Array */handle){        var t = handle[0];        cache[t] && d.each(cache[t], function(idx){            if(this == handle[1]){                cache[t].splice(idx, 1);            }        });    };})(jQuery);