0

I would like to ask to be able to build one, based on the the inherited patterns or syntactic sugar, it is not a large framework.Classification Ajax code to maintain the MVC architecture.

I hope it can operate, such as sample code, but I do not know Javascript can do this?

/* 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();
4

2 回答 2

0

在我看来,您正在寻找类似JavaScriptMVC的东西。

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

我建议您尝试下载 JavaScriptMVC 中的示例,看看它是否是您正在寻找的。

于 2013-02-09T12:14:33.163 回答
0

我创造了这个

//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();
于 2013-02-20T08:17:58.110 回答