/** * 广播 * * Created by GUY on 2015/12/23. * @class */ import { Controller } from "./0.controller"; export class BroadcastController extends Controller { init() { this._broadcasts = {}; } on(name, fn) { if (!this._broadcasts[name]) { this._broadcasts[name] = []; } this._broadcasts[name].push(fn); return () => this.remove(name, fn); } send(name) { const args = [].slice.call(arguments, 1); BI.each(this._broadcasts[name], (i, fn) => fn.apply(null, args)); } remove(name, fn) { if (fn) { BI.remove(this._broadcasts[name], (index, cb) => fn === cb); if (this._broadcasts[name].length === 0) { delete this._broadcasts[name]; } } else { delete this._broadcasts[name]; } return this; } }