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

定义 WxNotificationCenter.js 文件;

javascript 复制代码
/**
 * 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中引入该文件

javascript 复制代码
var WxNotificationCenter = require("../../utils/WxNotificationCenter.js");

// 广播:

WxNotificationCenter.postNotificationName('广播的名字', '');    

// 监听

 var that = this;

WxNotificationCenter.addNotification('广播的名字', that.'要调用的方法' , that);
相关推荐
腾马科技2 小时前
新版saas餐饮外卖小程序源码/微信/支付宝/抖音/扫码点餐/DIY装修/美团代付/全开源
微信小程序·小程序·点餐小程序
郑叔敲代码7 小时前
帝国cms 微信小程序的登录逻辑
微信小程序·小程序·notepad++
棋子一名10 小时前
跑马灯组件 Vue2/Vue3/uni-app/微信小程序
微信小程序·小程序·uni-app·vue·js
无衣 秦风11 小时前
vue3+hubuilderX开发微信小程序使用elliptic生成ECDH密钥对遇到的问题
微信小程序·小程序
游戏开发爱好者812 小时前
BShare HTTPS 集成与排查实战,从 SDK 接入到 iOS 真机调试(bshare https、签名、回调、抓包)
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088912 小时前
iOS 26 系统流畅度实战指南|流畅体验检测|滑动顺畅对比
android·macos·ios·小程序·uni-app·cocoa·iphone
流***陌13 小时前
陪诊就医小程序中健康档案的精细化管理设计方案
小程序
明天你好26713 小时前
如何做一个花店小程序,搭建一个小程序多少钱
微信小程序·小程序·模拟退火算法
2501_9151063214 小时前
苹果软件加固与 iOS App 混淆完整指南,IPA 文件加密、无源码混淆与代码保护实战
android·ios·小程序·https·uni-app·iphone·webview
江城开朗的豌豆14 小时前
小程序登录不迷路:一篇文章搞定用户身份验证
前端·javascript·微信小程序