EventFlow.helper.js 事件流程控制

/*!
 * 事件流程管理
 * version: 1.0.0-2018.07.25
 * Requires ES6
 * Copyright (c) 2018 Tiac
 * http://www.cnblogs.com/tujia/p/9369027.html
*/

class EventFlow
{
    static init()
    {
        this.objs         = [];
        this.events       = {};
        this.currentEvent = '';
        this.currentExp   = '';
    }

    static add(selector)
    {
        this.objs.push( document.querySelectorAll(selector) );
        return this;
    }

    static on(evt)
    {
        this.events[evt]  = [];
        this.currentEvent = evt;
        return this;
    }

    static when(exp)
    {
        this.currentExp = exp;
        return this;
    }

    static then(func)
    {
        this.events[this.currentEvent].push({
            'exp': this.currentExp,
            'func': func.toString().replace(/[^\{]+\{([\s\S]+)\}$/, '$1')
        });

        this.currentExp = '';
        return this;
    }

    static run()
    {
        if(this.objs.length>0)
        {
            let i = 0;
            for(let evt in this.events){
                let commands = '';
                let events   = this.events[evt];
                for(let i in events){
                    if(events[i]['exp']!=''){
                        commands += `if(${events[i]['exp']}){
                            ${events[i]['func']}
                        }`;
                    }else{
                        commands += events[i]['func'];
                    }
                }

                this.objs[i].forEach((item, i)=>{
                    item.addEventListener(evt, function(){
                        eval(commands);
                    });
                });

                i++;
            }
        }

        this.init();
    }
}

export default EventFlow;

 

执行效率并不高,当写来玩呗~

 

import EventFlow from './EventFlow.helper.js';

EventFlow.init();

EventFlow.add('.sel-type').on('change')
.when('this.value==1').then(function(){
    // code
})
.when('this.value==2').then(function(){
    // code
})
.when('this.value==3').then(function(){
    // code
})
.when('this.value==4').then(function(){
    // code
});

EventFlow.add('.inp-name').on('input')
.then(function(){
    // code
});

EventFlow.add('.inp-name').on('blur')
.then(function(){
    // code
});

EventFlow.run();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己封装的LocalStoreHelper.js,做页面存储的兄弟可以参考 var LocalStoreHelper = { CookieHelp: { //expire:分钟 SetCookie: function (cookiename, cookievalue, expire) { var today = new Date(); var expiredate = new Date(); expiredate.setTime(today + 60 * 1000 * expire); [removed] = cookiename + "=" + escape(cookievalue) + ";expires=" + expiredate.toGMTString(); }, GetCookie: function (cookiename) { if ([removed].length > 0) {  //先查询cookie是否为空,为空就return "" c_start = [removed].indexOf(cookiename + "=")  //通过String对象的indexOf()来检查这个cookie是否存在,不存在就为 -1   if (c_start != -1) { c_start = c_start + c_name.length + 1  //最后这个+1其实就是表示"="号啦,这样就获取到了cookie值的开始位置 c_end = [removed].indexOf(";", c_start)  //其实我刚看见indexOf()第二个参数的时候猛然有点晕,后来想起来表示指定的开始索引的位置...这句是为了得到值的结束位置。因为需要考虑是否是最后一项,所以通过";"号是否存在来判断 if (c_end == -1) c_end = [removed].length return unescape([removed].substring(c_start, c_end))  //通过substring()得到了值。想了解unescape()得先知道escape()是做什么的,都是很重要的基础,想了解的可以搜索下,在文章结尾处也会进行讲解cookie编码细节 } } return "" }, RemoveCookie: function (cookiename) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getCookie(cookiename); if (cval != null) [removed] = cookiename + "=" + cval + ";expires=" + exp.toGMTString(); } }, LocalData: { SetItem: function (name, value) { if (window.localStorage) { localStorage.setItem(name, value); } else { LocalStoreHelper.CookieHelp.SetCookie(name, value, 30); } }, GetItem: function (name) { if (window.localStorage) { return localStorage.getItem(name); } else { return LocalStoreHelper.CookieHelp.GetCookie(name); } }, DelItem:function(name) { if (window.localStorage) { return localStorage.removeItem(name); } else { return LocalStoreHelper.CookieHelp.GetCookie(name); } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值