ФRuŠKać

Event

constructor
Event()

Event

function Event() {
}

Event

prototype
Event.prototype

Event.prototype = {

publish

method
Event.prototype.publish()

Option name Type Description
name string
args

Publish on channel

publish: function (name, args) {
    try {
        cache[name] && cache[name].forEach(function (callback) {
            callback.apply(args);
        });
    } catch (err) {
        console.warn(err);
    }
},

subscribe

method
Event.prototype.subscribe()

Option name Type Description
name string, Array
callback Function

Subscribe a callback on a channel

subscribe: function (name, callback) {

    var self = this;

    if (name.constructor === Array) {
        name.forEach(function (n) {
            self.subscribe(n, callback);
        });
        return;
    }

    if (!cache[name]) {
        cache[name] = [];
    }
    cache[name].push(callback);
    return [name, callback];
},

unsubscribe

method
Event.prototype.unsubscribe()

Option name Type Description
handle

Unsubscribe

unsubscribe: function (handle) {
    var name = handle[0];
    cache[name] && cache[name].forEach(function (id) {
        if (this == handle[1]) {
            cache[name].splice(id, 1);
        }
    });
}

    };

    return Event;

})();