Git Product home page Git Product 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 liaojinghui avatar

Watchers

 avatar  avatar

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.