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

news/2024/7/20 1:27:00 标签: 微信小程序, 小程序

定义 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.niftyadmin.cn/n/4980066.html

相关文章

震动分析国标GB/T 19873.3-2019/ISO 13373-3:2015笔记

1.国家标准 1.1震动测量 现行国家标准是&#xff1a;GB/T 19873.2-2009 机器状态监测与诊断 振动状态监测 第2部分&#xff1a;振动数据处理、分析与描述 它的起草人&#xff1a; 郑州机械研究所。西安热工研究院有限公司。东南大学。 主要起草人 韩国明 、张学延 、傅行…

「Redis」1. 数据类型的底层实现

前言&#xff1a;在这篇博文中&#xff0c;我们将简单总结在面试中怎么回答Redis数据类型的底层实现。 因为面试时间就那么点&#xff0c;言简意赅的描述自己会的知识显得尤为重要‼️ 文章目录 0.1. String 的底层实现原理0.2. 列表的底层实现原理0.3. 字典的底层实现原理0.4.…

两个hdfs之间迁移传输数据

本文参考其他大数据大牛的博文做了整理和实际验证&#xff0c;主要解决hdfs跨集群复制/迁移问题。 在hdfs数据迁移时总会涉及到两个hdfs版本版本问题&#xff0c;致力解决hdfs版本相同和不同两种情况的处理方式&#xff0c;长话短说&#xff0c;进正文。 distcp: hadoop自带的…

【Flutter】Flutter 使用 flutter_timezone 获取当前操作系统的时区

【Flutter】Flutter 使用 flutter_timezone 获取当前操作系统的时区 文章目录 一、前言二、flutter_timezone 包的背景三、安装和基本使用四、深入理解时区五、实际业务中的用法六、完整示例七、总结 一、前言 大家好&#xff01;我是小雨青年&#xff0c;今天我想和大家分享一…

自定义String字符串工具类 StringUtils.java

自定义String字符串工具类 StringUtils.java 简介 自定义String字符串工具类 api 是否为空 checkEmpty(String str);目标字符串是目标数组中的一个 checkContains(String str, String[] target);限制最大长度 checkMaxLength(String str, Long l);是否纯数字的字符串 check…

Ansible 自动化运维工具的使用

目录 一、Ansible简介 二、Ansible 的安装和使用 1.下载 2.使用 三、Ansible命令和模块 1.命令格式 2.命令行模块 &#xff08;1&#xff09;command 模块 &#xff08;2&#xff09;shell 模块 &#xff08;3&#xff09;cron 模块 &#xff08;4&#xff09;user 模…

Docker部署UI自动化测试环境

这里写目录标题 一、分布式自动化测试要解决什么问题&#xff1f;二、分布式环境搭建1、环境准备2、创建容器3、启动容器4、查看容器运行状态5、查看Selenium hub 、node 启动状态6、启动VNC Server7、测试脚本 一、分布式自动化测试要解决什么问题&#xff1f; 分布式自动化测…

Docker Compose 安装使用 教程

Docker Compose 1.1 简介 Compose 项目是 Docker 官方的开源项目&#xff0c;负责实现对 Docker 容器集群的 快速编排 。从功能上看&#xff0c;跟 OpenStack 中的 Heat 十分类似。 其代码目前在 https://github.com/docker/compose 上开源。 Compose 定位是 「定义和运行多个…