Giter VIP home page Giter VIP logo

wxnotificationcenter's Introduction

WxNotificationCenter

WxNotificationCenter - 微信小程序通知广播模式类,降低小程序开发的耦合度

信息

来自微信小程序开发论坛 http://weappdev.com/ 垂直微信小程序开发论坛

版本信息

version 0.1

效果预览

WxNotificationCenter效果预览gif

使用

  1. copy 文件 WxNotificationCenter.js 到你的开发目录中

  2. 引入 WxNotificationCenter.js 在你需要的js

var WxNotificationCenter = require("../../WxNotificationCenter/WxNotificationCenter.js");
  1. 注册通知
// 最好在onLoad中进行
/**
* addNotification
* 注册通知对象方法
* 
* 参数:
* name: 注册名,一般let在公共类中
* selector: 对应的通知方法,接受到通知后进行的动作
* observer: 注册对象,指Page对象,可选,不填写的话在remove中会失效
*/

var that = this
WxNotificationCenter.addNotification("testNotificationName",that.testNotificationFn,that)
  1. 发送通知
WxNotificationCenter.postNotificationName("testNotificationName");
  1. 移除通知
// 移除通知在本也完成
WxNotificationCenter.removeNotification("testTabNotificationName",that)

特性

  • 支持注册、发送、移除通知
  • 支持传参,字典等

源码分析

  • var __notices = [];

存放通知数组

  • addNotification 注册方法

     * addNotification
     * 注册通知对象方法
     * 
     * 参数:
     * name: 注册名,一般let在公共类中
     * selector: 对应的通知方法,接受到通知后进行的动作
     * observer: 注册对象,指Page对象
     */
    
    
  • postNotificationName 发送方法

    /**
      * postNotificationName
      * 发送通知方法
      * 
      * 参数:
      * name: 已经注册了的通知
      * info: 携带的参数
      */
    
     function postNotificationName(name, info)
    
  • removeNotification 移除方法

    /**
     * removeNotification
     * 移除通知方法
     * 
     * 参数:
     * name: 已经注册了的通知
     * observer: 移除的通知所在的Page对象
     */
    
    function removeNotification(name,observer)
    

拓展阅读

  • 什么叫做通知广播模式

本类借鉴iOS开发中的NSNotificationCenter的消息模式进行开发

可阅读: 细说KVO & KVC & NSNotificationCenter那些事

  • 通知广播模式的使用场景
  • 多级页面传值

来源

来自微信小程序开发论坛 http://weappdev.com/ 垂直微信小程序开发论坛

wxnotificationcenter's People

Contributors

dicn avatar icindy avatar jim8y avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wxnotificationcenter's Issues

小程序页面中this值变化造成通知bug

A-B-C三个页面(navigateTo)方式跳转; 在B页面增加(每次add之前清除上一次添加的监听事件)addOnce事件监听 到C页面没有执行postNotice就依次返回A;再次从A-B,这个时候会发现this值变了!所以没法删除上一次添加的事件监听;造成在C页面中postNotice不能正在B页面中被正确执行;

Wepy框架使用通知时this会有问题

发现你的代码中使用了将page放在observer这样告成回调时this作用域会有问题,变成了{observer, selector, name}这样引用就不对了,而且没办法修改界面,所以我使改了你的代码
原来是这样
var newNotice ={ name: name, selector: selector, observer: observer };
修改后:
var newNotice = Object.assign(observer, {selector: selector}, {name: name}) /* { name: name, selector: selector, observer: observer };*/
合并成一个对像才适用于Wepy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.