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


    定义 WxNotificationCenter.js  文件; 

    1. /**
    2. * author: Di (微信小程序开发工程师)
    3. * organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com)
    4. * 垂直微信小程序开发交流社区
    5. *
    6. * github地址: https://github.com/icindy/WxNotificationCenter
    7. *
    8. * for: 微信小程序通知广播模式类,降低小程序之间的耦合度
    9. * detail : http://weappdev.com/t/wxnotificationcenter/233
    10. */
    11. // 存放
    12. var __notices = [];
    13. var isDebug = true;
    14. /**
    15. * addNotification
    16. * 注册通知对象方法
    17. *
    18. * 参数:
    19. * name: 注册名,一般let在公共类中
    20. * selector: 对应的通知方法,接受到通知后进行的动作
    21. * observer: 注册对象,指Page对象
    22. */
    23. function addNotification(name, selector, observer) {
    24. if (name && selector) {
    25. if(!observer){
    26. // ("addNotification Warning: no observer will can't remove notice");
    27. }
    28. var newNotice = {
    29. name: name,
    30. selector: selector,
    31. observer: observer
    32. };
    33. addNotices(newNotice);
    34. } else {
    35. }
    36. }
    37. /**
    38. * 仅添加一次监听
    39. *
    40. * 参数:
    41. * name: 注册名,一般let在公共类中
    42. * selector: 对应的通知方法,接受到通知后进行的动作
    43. * observer: 注册对象,指Page对象
    44. */
    45. function addOnceNotification(name, selector, observer) {
    46. if (__notices.length > 0) {
    47. for (var i = 0; i < __notices.length; i++) {
    48. var notice = __notices[i];
    49. if (notice.name === name) {
    50. if (notice.observer === observer) {
    51. return;
    52. }
    53. }
    54. }
    55. }
    56. this.addNotification(name, selector, observer)
    57. }
    58. function addNotices(newNotice) {
    59. // if (__notices.length > 0) {
    60. // for (var i = 0; i < __notices.length; i++) {
    61. // var hisNotice = __notices[i];
    62. // //当名称一样时进行对比,如果不是同一个 则放入数组,否则跳出
    63. // if (newNotice.name === hisNotice.name) {
    64. // if (!cmp(hisNotice, newNotice)) {
    65. // __notices.push(newNotice);
    66. // }
    67. // return;
    68. // }else{
    69. // __notices.push(newNotice);
    70. // }
    71. // }
    72. // } else {
    73. // }
    74. __notices.push(newNotice);
    75. }
    76. /**
    77. * removeNotification
    78. * 移除通知方法
    79. *
    80. * 参数:
    81. * name: 已经注册了的通知
    82. * observer: 移除的通知所在的Page对象
    83. */
    84. function removeNotification(name,observer) {
    85. for (var i = 0; i < __notices.length; i++){
    86. var notice = __notices[i];
    87. if(notice.name === name){
    88. if(notice.observer === observer){
    89. __notices.splice(i,1);
    90. return;
    91. }
    92. }
    93. }
    94. }
    95. /**
    96. * postNotificationName
    97. * 发送通知方法
    98. *
    99. * 参数:
    100. * name: 已经注册了的通知
    101. * info: 携带的参数
    102. */
    103. function postNotificationName(name, info) {
    104. if(__notices.length == 0){
    105. return;
    106. }
    107. for (var i = 0; i < __notices.length; i++){
    108. var notice = __notices[i];
    109. if(notice.name === name){
    110. notice.selector(info);
    111. }
    112. }
    113. }
    114. // 用于对比两个对象是否相等
    115. function cmp(x, y) {
    116. // If both x and y are null or undefined and exactly the same
    117. if (x === y) {
    118. return true;
    119. }
    120. // If they are not strictly equal, they both need to be Objects
    121. if (! (x instanceof Object) || !(y instanceof Object)) {
    122. return false;
    123. }
    124. // They must have the exact same prototype chain, the closest we can do is
    125. // test the constructor.
    126. if (x.constructor !== y.constructor) {
    127. return false;
    128. }
    129. for (var p in x) {
    130. // Inherited properties were tested using x.constructor === y.constructor
    131. if (x.hasOwnProperty(p)) {
    132. // Allows comparing x[ p ] and y[ p ] when set to undefined
    133. if (!y.hasOwnProperty(p)) {
    134. return false;
    135. }
    136. // If they have the same strict value or identity then they are equal
    137. if (x[p] === y[p]) {
    138. continue;
    139. }
    140. // Numbers, Strings, Functions, Booleans must be strictly equal
    141. if (typeof(x[p]) !== "object") {
    142. return false;
    143. }
    144. // Objects and Arrays must be tested recursively
    145. if (!Object.equals(x[p], y[p])) {
    146. return false;
    147. }
    148. }
    149. }
    150. for (p in y) {
    151. // allows x[ p ] to be set to undefined
    152. if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
    153. return false;
    154. }
    155. }
    156. return true;
    157. };
    158. module.exports = {
    159. addNotification: addNotification,
    160. removeNotification: removeNotification,
    161. postNotificationName: postNotificationName,
    162. addOnceNotification: addOnceNotification
    163. }

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

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

  • 相关阅读:
    Go fsnotify简介
    抓包工具总结对照【fiddler F12 Charles wireshark】
    使用NVM管理nodejs
    19.请介绍一下重绘和回流
    etcd MVCC 存储结构及流程
    Gismo compile note
    网站被篡改的主要方式有几种?
    Python 装饰器、嵌套函数、高阶函数
    3-1:Tomcat介绍、Mac版安装与使用及Tomcat目录文件详解
    JS逆向实战27——pdd的anti_content 分析与逆向
  • 原文地址:https://blog.csdn.net/yinge0508/article/details/132532723