zoukankan      html  css  js  c++  java
  • PureMVC(JS版)源码解析(三):Observer类

          上一篇博客中,我们讲到了Notification类(消息类),Notification(消息)是连接观察者(observer)和通知者(notifier)之间的桥梁。这一篇博客,主要是在代码层面上讲一下observer类。

           通过源码,我们可以看出Observer类有两个属性,分别是notify和context,notify是函数类型,context是object类型,其中notify是Observer对象(观察者)接收到消息之后执行的回调函数,context是回调函数的执行作用域。 

    /**
     * The Observers callback Function
     * 
     * @private
     * @type {Function}
     */
    Observer.prototype.notify= null;
    
    /**
     * The Observers callback Object
     * @private
     * @type {Object}
     */
    Observer.prototype.context= null;

    这两个属性,分别有get/set方法。[setNotifyMethod()/getNotifyMethod()/setNotifyContext()/getNotifyContext()];

    Observer在构造函数中对这两个属性进行了初始化:

    function Observer (notifyMethod, notifyContext)
    {
        this.setNotifyMethod(notifyMethod);
        this.setNotifyContext(notifyContext);
    };

    此外,Observer类还有一个很重要的方法,就是Observer对象接收到消息执行的notifyObserver()方法:
    /**
     * Notify the interested object.
     * 
     * @param {puremvc.Notification} notification
     *  The Notification to pass to the interested objects notification method
     * @return {void}
     */
    Observer.prototype.notifyObserver= function (notification)
    {
        this.getNotifyMethod().call(this.getNotifyContext(), notification);  
    };

    上面的代码的意思是在context(回调函数执行作用域)中调用notify方法(回调函数)。

    关于Observer类基本上就这几点了,一是notify和context两个属性,二是构造函数中对两个属性的初始化,三是notifyObserver()。接下来的博客会讲notifier(通知者类)。

    最后,同样分享一张之前学PureMVC做的思维导图:

  • 相关阅读:
    微信JS SDK Demo
    微信jssdk常见错误及解决方法
    多机定时任务处理
    python zip压缩文件 并移动到指定目录
    Nginx + Uswgi + Django的部署
    pycharm 安装第三方库报错:AttributeError: 'module' object has no attribute 'main'
    zabbix监控
    转:老张喝茶 教你同步异步 阻塞与非阻塞
    odoo开发笔记 -- 多对多字段追加数据
    xml文件对比工具推荐:Altova XMLSpy 2013
  • 原文地址:https://www.cnblogs.com/iRavior/p/3353595.html
Copyright © 2011-2022 走看看