在小程序環(huán)境中是不能偷懶了,需要把之前的代碼改寫一下。要把 Page 對(duì)象也傳給調(diào)度中心保存起來,作為回調(diào)函數(shù)調(diào)用時(shí)的上下文對(duì)象。
代碼:
//event.jsclassEvent{ style="color: #333333; font-weight: bold;">if (typeof fn != "function") { console.error('fn must be a function') return } this._stores = this._stores || {} ;(this._stores[event] = this._stores[event] || []).push({cb: fn, ctx: ctx}) } emit (event) { this._stores = this._stores || {} var store = this._stores[event], args if (store) { store = store.slice(0) args = [].slice.call(arguments, 1) for (var i = 0, len = store.length; i < len; i++) { store[i].cb.apply(store[i].ctx, args) } } } off (event, fn) { this._stores = this._stores || {} // all if (!arguments.length) { this._stores = {} return } // specific event var store = this._stores[event] if (!store) return // remove all handlers if (arguments.length === 1) { delete this._stores[event] return } // remove specific handler var cb for (var