JavaScript 簡(jiǎn)單的MVC+Router模式實(shí)現(xiàn)


簡(jiǎn)單的MVC+Router模式實(shí)現(xiàn),可能實(shí)現(xiàn)得不太好,先記錄下來(lái),以后再改....


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>基于MVC+router基本原理實(shí)現(xiàn)</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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評(píng)論 25 709
  • 路由是實(shí)現(xiàn)模塊間解耦的一個(gè)有效工具。如果要進(jìn)行組件化開(kāi)發(fā),路由是必不可少的一部分。目前iOS上絕大部分的路由工具都...
    黑超熊貓zuik閱讀 4,080評(píng)論 8 52
  • 定期壽險(xiǎn),是消費(fèi)型,跟車險(xiǎn)一個(gè)性質(zhì)。定期是指人生某一個(gè)階段,不同家庭,這個(gè)時(shí)期不同。 有兩個(gè)人群特別適合。1是經(jīng)濟(jì)...
    覃大小姐閱讀 252評(píng)論 0 1
  • 巫女在電話里說(shuō)準(zhǔn)備負(fù)責(zé),撩一個(gè)小男人撩出了真心我說(shuō)春天真好,盡管它離我尚有三千公里 他們說(shuō)藍(lán)圖和擴(kuò)張,我沒(méi)有想到殖...
    余上仙閱讀 461評(píng)論 27 23
  • 悲傷,就是一株小蘭花。 我用珠子鋪滿糖果瓶的底部,把水灌進(jìn)去,放一株蘭花,一株綠蘿,我擺弄著它們,想象將來(lái)它們會(huì)長(zhǎng)...
    瀟昳閱讀 297評(píng)論 0 0

友情鏈接更多精彩內(nèi)容