Javascript可以构建这种MVC模式或语法吗?

Javascript可以构建这种MVC模式或语法吗?,javascript,model-view-controller,Javascript,Model View Controller,我想要求能够构建一个,基于继承的模式或语法糖,它不是一个大框架。分类Ajax代码来维护MVC架构 我希望它能运行,比如示例代码,但我不知道Javascript能做到这一点吗 /* Just for demonstration architecture may be constructed with an object or function */ var Prototype = (){ this.Controller = {/*...*/}; this.Model = {/*..

我想要求能够构建一个,基于继承的模式或语法糖,它不是一个大框架。分类Ajax代码来维护MVC架构

我希望它能运行,比如示例代码,但我不知道Javascript能做到这一点吗

/* Just for demonstration architecture may be constructed with an object or function */
var Prototype = (){
    this.Controller = {/*...*/};
    this.Model = {/*...*/};
    this.View = {/*...*/};
    this.Constructor = function(){/* auto binding, like run this.Controller.update */};
    /*...*/
};

var a = new Prototype;  /* or $.extend, object.create, etc */

/* Create a function */
a.Controller.update = function(){
    $('#button').bind('click', function(){
        this.Model.update();    // Equivalent a.Model.update()
        this.Model.list();      // Equivalent a.Model.list()
        b.Model.del();          // Can call other instance of the function
    });
}
a.Model.update = function(){
    $.ajax('json source', function(data){
        this.View.update('json parse'); // Equivalent a.View.update()
    });
}
a.View.update = function(obj){
    /* Do something about the DOM */
}

/* Can be controlled by the other controller */
a.Model.list = function(){
    this.View.list('json parse');   // Equivalent a.View.list()
}
a.View.list = function(obj){
    /* Do something about the DOM */
}


var b = new Prototype;
b.Controller.del = function(){
    $('#del').bind('click', function(){
        this.Model.del();   // Equivalent b.Model.del()
    }
}
b.Model.del = function(){
    $.ajax('json source', function(data){
        this.View.del('json parse');    // Equivalent b.View.del()
    });
}
b.View.del = function(){
    /* Do something about the DOM */
}


a.Constructor();    //init
b.Constructor();

我觉得你在找类似的东西

我实际上在工作中使用它,它可以很好地将不同的控制器分开

我建议您尝试JavaScriptMVC下载中的示例,看看它是否是您想要的

//framework
var Jmvc = function BellaJMVC(){
    this.Controller = { };
    this.Model = function(uri, action, data){
        var self = this;
        $.ajax({ "type": "POST", "url": uri, "data": data, 
            "success": function(r){ self.View[action]($.parseJSON(r)); },
            "error": function(r){ /*...*/ }
        });
    };
    this.View = { };
    this.Init = function(){
        var fNOP = function () {};
        fNOP.prototype = this.prototype;
        for(var cAction in this.Controller){
            this.Controller[cAction].apply(this);
            this.Controller[cAction].prototype = new fNOP;
        }
    };
}

//apply
var a = new Jmvc();
a.Controller.update = function Cupdate(){
    var self = this;
    $('#updateBtn').bind('click', function(){
        self.Model('ajax/json.html', 'update', {"test":"1234"});
    });
}
a.View.update = function Vupdate(obj){
    $('#update').html(obj.msg);
}
a.Init();