0.1.0 • Published 5 years ago

@cleverbamboo/event-bus v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
5 years ago

具体调用方法

App 是小程序的实例,在每个 Page 里都能通过执行 getApp 函数获取到它。我们可以把 Event 类的实例挂载在 App 中,方便每个 Page 去调用。

// app.js

const Event = require('./libs/event')

App({
    event: new Event(),
    ...
})

订单列表页在 onLoad 生命周期中订阅 “afterPaySuccess” 事件。

//order_list.js

var App = getApp()

Page({
    onLoad: function(){
        App.event.on('afterPaySuccess',this.afterPaySuccess, this)
    },
    afterPaySuccess: function(orderId) {
        // do sth
    },
})

在订单详情页支付成功的回调中,发布 “afterPaySuccess” 事件,同时带上订单 id 参数。

//order_detail.js

var App = getApp()

Page({
    raisePayment: function() {
        App.event.emit('afterPaySuccess', orderId)
    },
})

所有 Page 的 onUnload 生命周期,必须注销掉之前订阅的事件。注销方法 off 的调用姿势有三种,不过还是建议注销当前 Page 所订阅的事件,而不是注销所有的。 tip: 在那里订阅就在注销

var App = getApp()

Page({
    onLoad: function(){
        App.event.on('afterPaySuccess',this.afterPaySuccess, this)
    },
    afterPaySuccess: function(orderId) {
        // do sth
    },
    onUnload: function(){
        // remove all
        App.event.off()
        // remove all callbacks
        App.event.off('afterPaySuccess')
        // remove specific callbacks
        App.event.off('afterPaySuccess', this.afterPaySuccess)
    }
})