RubyLouvre / mass-Framework

一个模块化,面向大规模开发的javascript框架
MIT License
652 stars 534 forks source link

subpub #56

Closed RubyLouvre closed 11 years ago

RubyLouvre commented 11 years ago
                (function(window, doc, undef) {

                    var topics = {},
                            subUid = -1,
                            pubsubz = {};

                    pubsubz.publish = function(topic, args) {

                        if (!topics[topic]) {
                            return false;
                        }

                        setTimeout(function() {
                            var subscribers = topics[topic],
                                    len = subscribers ? subscribers.length : 0;

                            while (len--) {
                                subscribers[len].func(topic, args);
                            }
                        }, 0);

                        return true;

                    };

                    pubsubz.subscribe = function(topic, func) {

                        if (!topics[topic]) {
                            topics[topic] = [];
                        }

                        var token = (++subUid).toString();
                        topics[topic].push({
                            token: token,
                            func: func
                        });
                        return token;
                    };

                    pubsubz.unsubscribe = function(token) {
                        for (var m in topics) {
                            if (topics[m]) {
                                for (var i = 0, j = topics[m].length; i < j; i++) {
                                    if (topics[m][i].token === token) {
                                        topics[m].splice(i, 1);
                                        return token;
                                    }
                                }
                            }
                        }
                        return false;
                    };

                    getPubSubz = function() {
                        return pubsubz;
                    };

                    window.pubsubz = getPubSubz();

                }(this, this.document));
RubyLouvre commented 11 years ago
      
    var   fx2 =   "width:attr(width);\
               height:attr(height);\\n\
               margin-left:attr(margin-left);\
               margin-right:attr(margin-right);\
               margin-top:attr(margin-top);\
               margin-bottom:attr(margin-bottom);\
               padding-left:attr(padding-left);\
               padding-right:attr(padding-right);\
               padding-top:attr(padding-top);\
               padding-bottom:attr(padding-bottom);\
               border-left-width:attr(border-left-width);\
               border-right-width:attr(border-right-width);\
               border-top-width:attr(border-top-width);\
               border-bottom-width:attr(border-bottom-width);"
//上面代码可以由下面两个函数生成
    function getCurrentStyle(name) {
        return  name + ":attr(" + name + ");"
    }
    function LoopEdge(callback) {
        var dir = ["-top", "-bottom", "-left", "-right"]
        $.each({
            margin: "",
            padding: "",
            border: "-width"
        }, function(prefix, suffix) {
            for (var i = 0; i <  4; i++) {
                callback(prefix + dir[i] + suffix);
            }
        });
    }