Using the MVC template in a small JavaScript + jQuery game


You need to develop a small game extension for Chrome (like minesweeper). The approximate code size is about 400 lines. What is the best way to do this in the form of a single continuous script, or to break all the functionality into a model, view, and controller and put their functionality in three different scripts. Is it worth it?

Author: stanislav, 2010-12-20

1 answers

The main purpose of using the MVC template is to provide automatic testing of the user interface. If this is important for your game application, then, yes, it is better to split even for such a small code into modules (although they can be safely placed in a single file). It should be borne in mind that the browser application itself already contains a lot of MVC: the view-HTML markup, the controller-JavaScript event handlers, the model-logic (in your case game room). An example, see below.

Markup:

<body onload="View.Init();">

<input name="get" type="text" />
<input name="put" type="button" />

</body>

Code:

var View = {
    Init: function () {
        // Привязка событий
        $('#put').click(function () {
            Controller.Put(); 
        });

        // Привязка данных
        $('#get').bind('get', function () {
            $('#get').text(Model.Get());
        });
    }
}

var Controller = {
    // Стимул
    Put: function () {
        $('#get').trigger('get');
    }
}

var Model = {
    // Отклик
    Get: function () {
        return 'responce';
    }
}
 7
Author: stanislav, 2010-12-30 12:44:44