This article describes how to implement one-to-multiple message sending and related information about the instance code in the applet. For more information, see
Implementation of one-to-multiple message sending and instance code in the applet
The passing of values and notifications between interfaces in the applet is quite painful. Therefore, it imitates the Notification Center in iOS and writes a set of similar notification centers in the applet.
The notification center can send one-to-multiple messages and deliver objects. Simple to use.
Register a notification name on the interface for receiving messages. Then, post the notification name on the interface for sending the message. You can register the same notification name on multiple interfaces. In this way, you can send one-to-multiple messages.
Usage:
1: reference notification. js in app. js
Var icationicationcenter = require ('/utils/notification. js'); // change it to your absolute path.
2: Add the following in app. js:
App({ onLaunch: function (){ this.notificationCenter = notificationCenter.center(); }, notificationCenter:null,})
3: Register in page. js that receives the notification
PageA. js:
Var app = getApp (); Page ({onLoad: function (options) {app. icationicationcenter. register ("one notification name", this, "didReceviceAnyNotification") ;}, didReceviceAnyNotification: function (name, content) {console. log ("received notification:", name, content );},})
4: page. js that sends the notification
Arbitrary PageB. js functions
Var app = getApp (); Page ({anyFunction: function () {app. icationicationcenter. post ("notification name", {// any notification object });},})
Implementation:
Http://xiazai.jb51.net/201702/yuanma/wxappNotificationCenter-master (jb51.net).rar
Var icationicationcenter = {icationicationcenter :{}, // register a listener with the notification center. // Name: Listener notification name // observer: Listener // action: method name called when the listener receives the notification, // func: The function called when the listener receives the notification, // action func 2 Select 1 register: function (name, observer, action, func) {if (! Name |! Observer) return; if (! Action &&! Func) return; console. log ("registration notification:", name, observer); var center = this. icationicationcenter; var objects = center [name]; if (! Objects) {objects = [];} this. remove (name, observer); objects. push ({observer: observer, action: action, func: func}); center [name] = objects ;}, // remove a listener from the Notification center remove: function (name, observer) {if (! Name |! Observer) return; var center = this. icationicationcenter; var objects = center [name]; if (! Objects) {return;} var idx; var object; for (idx = 0; idx
Thank you for reading this article. I hope it will help you. thank you for your support for this site!
For more articles about how to implement one-to-multiple message sending in small programs, please follow the PHP Chinese network!