import * as $ from "jquery"; import { Mixin } from "../Mixin"; import * as _util_Observer from "../util/Observer"; export namespace mixin { /** * Observable Class */ export class Observable extends Mixin { public subscribes: Object = {}; /** * The on method is alias for addListener. */ public on(ev: any, callback?: Function, scope?: any) { let observer = ev; if (typeof ev == "string") { let fn = callback || function () { }; observer = new _util_Observer.util.Observer(ev, fn); } this.subscribes[observer.name] = this.subscribes[observer.name] || []; this.subscribes[observer.name].push(observer); } /* public un(ev: string, callback: Function) { // $(this).unbind(ev, callback); } */ public fireEvent(ev: string) { let subscribers = this.subscribes[ev] || []; let args = Array.prototype.slice.call(arguments, 1); args.splice(0, 0, null); for (let i = 0; i < subscribers.length; i++) { setTimeout(Function.prototype.bind.apply(subscribers[i].fn, args), 0); } } } } // EOF