JavaScript 简单的MVC+Router模式实现


简单的MVC+Router模式实现,可能实现得不太好,先记录下来,以后再改....


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>基于MVC+router基本原理实现</title>
</head>
<body>
<div id="root"></div>
</body>
<script>
class Router {
    constructor(){
        this.routerFunction = {};
        this.showFunction = function(){
            return this.routerFunction;
        }
    }
    add(router,fn){
        if(!this.routerFunction[router]){
            this.routerFunction[router] = fn;
        }else{
            throw new Error(`${router}已存在`);
        }
    }
    del(router){
        if(this.routerFunction[router]){
            delete this.routerFunction[router];
        }else{
            throw new Error(`${router}不存在`);
        }
    }
    init(){
        window.addEventListener('load', () => {
            this.currentUrl = location.hash.slice(1) || '/';
            if(this.currentUrl === '/'){
                this.routerFunction[this.currentUrl]();
            }else{
                this.routerFunction['other'](this.currentUrl)
            }
        },false);
        window.addEventListener('hashchange', () => {
            this.currentUrl = location.hash.slice(1) || '/';
            if(this.currentUrl === '/'){
                this.routerFunction[this.currentUrl]();
            }else{
                this.routerFunction['other'](this.currentUrl)
            }
        },false)
    }
}

class Model{
    constructor(){
        this.value = {};
        this.showValue = function () {
            return this.value;
        }
    }
    add(valueName,value){
        if(!this.value[valueName]){
            this.value[valueName] = value;
        }else{
            throw new Error(`${valueName}已存在`)
        }
    }
    del(valueName,value){
        if(this.value[valueName]){
            this.value[valueName] = value;
        }else{
            throw new Error(`${valueName}不存在`)
        }
    }
    change(valueName,value){
        if(this.value[valueName] !== value){
            this.value[valueName] = value;
            control.change(valueName);
            return true;
        }else{
            return false;
        }
    }
}

class View{
    constructor(parent){
        this.parent = parent;
        this.template = {};
        this.showTemplate = function () {
            return this.template;
        }
    }
    add(templateName,template){
        if(!this.template[templateName]){
            this.template[templateName] = template;
        }else{
            throw new Error(`${templateName}已存在`)
        }
    }
    del(templateName){
        if(this.template[templateName]){
            delete this.template[templateName];
        }else{
            throw new Error(`${templateName}不存在`)
        }
    }
    mix(templateName,model){
        return this.template[templateName](model.value[templateName])
    }
    change(templateName,model){
        let children = document.getElementById(this.parent).children;
       for(let i = 0;i < children.length;i++){
           for(let attr of children[i].attributes){
               if(attr.nodeName.indexOf('z-') >= 0){
                   functionMix[attr.nodeName.slice(2)].call(children[i],model.value[templateName])
               }
           }
       }
    }
    init(){
        for(let props in this.template){
            document.getElementById(this.parent).innerHTML = this.mix(props,model);
        }
    }
}

class Controller{
    constructor(){
        this.control = {};
        this.showControl = function () {
            return this.control;
        }
    }
    add(name,fn){
        if(!this.control[name]){
            this.control[name] = fn;
        }else{
            throw new Error(`${name}已存在`)
        }
    }
    del(name){
        if(this.control[name]){
            delete this.control[name];
        }else{
            throw new Error(`${name}不存在`)
        }
    }
    change(name){
        view.change(name,model);
    }
    init(){
        for(let prop in this.control)this.control[prop]();
    }
}

const functionMix = {
    text:function(value) {
        this.innerText = value;
    },
    value:function(value){
        this.value = value;
    }
};

let model = new Model();
model.add('input','hello moonburn');

let view = new View('root');
view.add('input',function (value) {
    return `<input id="input" z-value="0" value="${value}"/><span z-text="0">${value}</span>`
});

let control = new Controller();
control.add('input',function () {
    let _dom = document.querySelector('#input');
    _dom.addEventListener('input',(event)=>{
        model.change('input',event.target.value)
    })
});

const router = new Router();
router.add('/',function () {
    model.change('input',model.value['input'])
});
router.add('other',function (value) {
    model.change('input',value)
});


view.init();
control.init();
router.init();
</script>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,446评论 25 708
  • 路由是实现模块间解耦的一个有效工具。如果要进行组件化开发,路由是必不可少的一部分。目前iOS上绝大部分的路由工具都...
    黑超熊猫zuik阅读 3,981评论 8 52
  • 定期寿险,是消费型,跟车险一个性质。定期是指人生某一个阶段,不同家庭,这个时期不同。 有两个人群特别适合。1是经济...
    覃大小姐阅读 214评论 0 1
  • 巫女在电话里说准备负责,撩一个小男人撩出了真心我说春天真好,尽管它离我尚有三千公里 他们说蓝图和扩张,我没有想到殖...
    余上仙阅读 387评论 27 23
  • 悲伤,就是一株小兰花。 我用珠子铺满糖果瓶的底部,把水灌进去,放一株兰花,一株绿萝,我摆弄着它们,想象将来它们会长...
    潇昳阅读 278评论 0 0