“Hello World” in MVC Pattern

后端 未结 5 1390
挽巷
挽巷 2021-01-29 19:18

In an interview for some company, I was asked this question.

What design patterns do you know...then I was told to write simplest \"hello world\" application based on MV

5条回答
  •  情深已故
    2021-01-29 19:30

    Better Example

    var M = {}, V = {}, C = {};
    
    /* Model View Controller Pattern with Form Example */
    
    
    /* Controller Handles the Events */
    
    M = {
        data: {
            userName : "Dummy Guy",
            userNumber : "000000000"
        }, 
        setData : function(d){
            this.data.userName = d.userName;
            this.data.userNumber = d.userNumber;
        },
        getData : function(){
            return data;
        }
    }
    
    V = {
        userName : document.querySelector("#inputUserName"),
        userNumber : document.querySelector("#inputUserNumber"),
        update: function(M){
            this.userName.value = M.data.userName;
            this.userNumber.value = M.data.userNumber;
        }
    }
    
    C = {
        model: M,
        view: V,
        handler: function(){
            this.view.update(this.model);
        }
    }
    
    document.querySelector(".submitBtn").addEventListener("click", function(){
        C.handler.call(C);
    }); 
    
    /* Model Handles the Data */
    
    /* View Handles the Display */
    

提交回复
热议问题