当前位置: 首页 > news >正文

微信小程序中的 广播监听事件

定义 WxNotificationCenter.js  文件; 

/*** author: Di (微信小程序开发工程师)* organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com)*               垂直微信小程序开发交流社区* * github地址: https://github.com/icindy/WxNotificationCenter* * for: 微信小程序通知广播模式类,降低小程序之间的耦合度* detail : http://weappdev.com/t/wxnotificationcenter/233*/
//   存放
var __notices = [];
var isDebug = true;
/*** addNotification* 注册通知对象方法* * 参数:* name: 注册名,一般let在公共类中* selector: 对应的通知方法,接受到通知后进行的动作* observer: 注册对象,指Page对象*/
function addNotification(name, selector, observer) {if (name && selector) {if(!observer){// ("addNotification Warning: no observer will can't remove notice");}var newNotice = {name: name,selector: selector,observer: observer};addNotices(newNotice);} else {}
}/*** 仅添加一次监听* * 参数:* name: 注册名,一般let在公共类中* selector: 对应的通知方法,接受到通知后进行的动作* observer: 注册对象,指Page对象*/
function addOnceNotification(name, selector, observer) {if (__notices.length > 0) {for (var i = 0; i < __notices.length; i++) {var notice = __notices[i];if (notice.name === name) {if (notice.observer === observer) {return;}}}}this.addNotification(name, selector, observer)
}function addNotices(newNotice) {// if (__notices.length > 0) {//     for (var i = 0; i < __notices.length; i++) {//         var hisNotice = __notices[i];//         //当名称一样时进行对比,如果不是同一个 则放入数组,否则跳出//         if (newNotice.name === hisNotice.name) {//             if (!cmp(hisNotice, newNotice)) {//                 __notices.push(newNotice);//             }//             return;//         }else{//             __notices.push(newNotice);//         }//     }// } else {// }__notices.push(newNotice);
}/*** removeNotification* 移除通知方法* * 参数:* name: 已经注册了的通知* observer: 移除的通知所在的Page对象*/function removeNotification(name,observer) {for (var i = 0; i < __notices.length; i++){var notice = __notices[i];if(notice.name === name){if(notice.observer === observer){__notices.splice(i,1);return;}}}}/*** postNotificationName* 发送通知方法* * 参数:* name: 已经注册了的通知* info: 携带的参数*/function postNotificationName(name, info) {if(__notices.length == 0){return;}for (var i = 0; i < __notices.length; i++){var notice = __notices[i];if(notice.name === name){notice.selector(info);}}}// 用于对比两个对象是否相等
function cmp(x, y) {// If both x and y are null or undefined and exactly the same  if (x === y) {return true;}// If they are not strictly equal, they both need to be Objects  if (! (x instanceof Object) || !(y instanceof Object)) {return false;}// They must have the exact same prototype chain, the closest we can do is  // test the constructor.  if (x.constructor !== y.constructor) {return false;}for (var p in x) {// Inherited properties were tested using x.constructor === y.constructor  if (x.hasOwnProperty(p)) {// Allows comparing x[ p ] and y[ p ] when set to undefined  if (!y.hasOwnProperty(p)) {return false;}// If they have the same strict value or identity then they are equal  if (x[p] === y[p]) {continue;}// Numbers, Strings, Functions, Booleans must be strictly equal  if (typeof(x[p]) !== "object") {return false;}// Objects and Arrays must be tested recursively  if (!Object.equals(x[p], y[p])) {return false;}}}for (p in y) {// allows x[ p ] to be set to undefined  if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {return false;}}return true;
};module.exports = {addNotification: addNotification,removeNotification: removeNotification,postNotificationName: postNotificationName,addOnceNotification: addOnceNotification
}

在需要的页面js中引入该文件

var WxNotificationCenter = require("../../utils/WxNotificationCenter.js");// 广播:WxNotificationCenter.postNotificationName('广播的名字', '');    // 监听var that = this;WxNotificationCenter.addNotification('广播的名字', that.'要调用的方法' , that);

http://www.lryc.cn/news/144226.html

相关文章:

  • Quickstart: MinIO for Linux
  • Java中word转Pdf工具类
  • 【conda install】网络慢导致报错CondaHTTPError: HTTP 000 CONNECTION FAILED for url
  • 2023-8-28 图中点的层次(树与图的广度优先遍历)
  • 设计模式(一)
  • Prometheus关于微服务的监控
  • CSS实现白天/夜晚模式切换
  • selenium实现输入数字字母验证码
  • Docker的运用
  • 在项目中快速搭建机器学习的流程
  • 计网-All
  • Rabbitmq的Federation Exchange
  • AIGC - 生成模型
  • 如何优雅地创建一个自定义的Spring Boot Starter
  • Hbase--技术文档--单机docker基础安装(非高可用)
  • React 生命周期新旧对比
  • 云计算存储类型
  • javacv基础03-调用本机摄像头并截图保存到本地磁盘
  • Python读取Windows注册表的实战代码
  • macOS 安装 Homebrew 详细过程
  • 数据结构之树型结构
  • 指针进阶详解
  • QGIS 如何添加天地图
  • PHP8内置函数中的数学函数-PHP8知识详解
  • 云计算企业私有云平台建设方案PPT
  • ORA-01174: DB_FILES be compatible RAC rolling fashion complete outage
  • 线性代数(五) 线性空间
  • kafka--技术文档--spring-boot集成基础简单使用
  • 【核磁共振成像】部分傅里叶重建
  • React中的flushSync与Vue中的nextTick的比较