zoukankan      html  css  js  c++  java
  • smartJS 0.1 API 讲解

    上篇简单的介绍smartjs了一些通用方法的api。这篇介绍基础的PromiseEvent(这个名字一直没想好,以前准备用callbacks的,但避免与jquery混淆,st的命名空间可以直接挂到$上)

    PromiseEvent

    基于事件和promise的回调管理,类似于jquery的callbacks,但具有结果传递,优先级,事件参数,promise控制等功能

    接口方法

    var events = st.promiseEvent(mode);
    
    events.add(name,function(e,arg,……){
        
    },priority,eventMode)
    
    event.fire(arg,...);


    参数说明

    mode :once和callback两种模式,(callback模式不会加入事件参数)

    e.stopProgation() 阻止后续回调

     

        event.add(name,fn,priority,eventMode) 添加事件回调, name :加入的事件回调名称; priority :权重设置 ;eventMode :加入的事件模式;once;

      event.fire(arg,,,,) 执行事件回调

      event.fireWith(context,[args]) 使用上下文回调

      event.has(name) 根据回调名称判断是否已注册回调

      event.remove(name) 根据名称删除回调

      event.clear()   清除所有回调

      

      e事件参数

    e.result 上一个回调的结果

    e.remove() 删除当前回调

    e.promise() 返回契约

    e.resolve() 解决契约

    e.reject() 拒绝契约

    使用样例

    普通方式

        var calls = st.promiseEvent(),
            result = [];
    
        //测试使用once模式,执行一次既销毁
        calls.add('call1', function(e, text) {
            result.push(text+'1');
        },"once")
    
        calls.add('call2', function(e, text) {
            //效果同“once”
            e.remove()
            result.push(text+'2');
        })
    
        //执行,结果 [call1,call2]
        calls.fire('call');
        
    

    权重,默认权重为0,可以通过st.conf({defPriority:100})来设置,相同权重先入先出

     result = [];
        //权重
        calls.add('p', function(e, text) {
            result.push('def');
        })
    
        calls.add('p10', function(e, text) {
            result.push(10);
        },10)
    
        calls.add('p50', function(e, text) {
            result.push(50);
        },50)
        
        calls.add('pDef', function(e, text) {
            result.push('def2');
        })
        
        //执行,结果 [50,10,def,def2]
        calls.fire();

    stopProgation,停止后续回调

    //stopProgation,停止后续回调
        calls.add('stop',function(e){
            e.stopPropagation();
            result.push("stop");
        },20)
        
        //执行,结果 [50,stop]
        calls.fire();


    结果传递,promiseEvent回将return的结果或者resolve的非undefined的结果记录下来并向下传递;

        //result传递模式
        calls.add('c1', function(e, text) {
            return text + "-c1";
        })
    
        calls.add('c2', function(e, text) {
            return e.result + "-c2";
        })
    
        calls.add('c3', function(e, text) {
           return e.result + "-c3";
        })
        
        //执行,结果 test-c1-c2-c3
        calls.fire('test');

    Prmose模式,

    可以与jquery的promise结合使用 

    $.when(calls.fire()).done(function(result){

    })

    //清除回调
        calls.clear();
        result = [];
    
        //promise模式
        calls.add("c1", function(e) {
            setTimeout(function() {
                result.push("c1");
                e.resolve();
            }, 100);
            return e.promise();
        });
    
        calls.add("c2", function(e) {
            result.push("c2");
        });
    
        //执行,结果 [c1,c2]
        calls.fire();


    mode设置,once(执行及销毁)和callback(简单回调)

    //callback模式 & once模式
        var calls2 = st.promiseEvent("callback once");
    
        //callback不存在e事件参数,只是具有见的回调管理
        calls2.add('c1', function(text) {
            return text + "-c1";
        })
    
        //执行第一次,结果 test-c1
        calls2.fire('test');
    
        //执行第二次,因为once模式,结果 undefined
        calls2.fire('test');


    更多的例子请参考smartjs上的测试用例

      

      

  • 相关阅读:
    php去除数组中重复值,并返回结果!
    SignalR 2 入门
    SignalR支持的平台
    SignalR简介
    作业调度系统quartz.net
    Oracle安装心得
    maven的项目目录解析
    web.xml的<url-parttern>的匹配规则
    Web.xml中四种验证方式
    Web.xml
  • 原文地址:https://www.cnblogs.com/zhh8077/p/3775892.html
Copyright © 2011-2022 走看看