Uncategorized

Revised Ext Model Creation from Sequelize Model Definitions in Node.js

I posted the other day about Automated ExtJS Model Creation using Sequelize Model Definitions and while the code works really well it occurred to me later that I might want to also be able to create static model definitions (for stuff like UI components, menus and the like.)

In order to achieve this we can use the fs module to test for the presence of a “file override” at the appropriate location. In the event that we find something at the destination we simply read the file and spit the contents back out using the ExpressJS response object.

The previous code that handled routing for /app/model/BLOB.js:

router.route("/app/model/:modelname")
    .get(
        function (req, res) {
            "use strict";
            var modelName = req.params.modelname.replace(/.js/, "");
            res.send(router.app.tools.buildExtModel(modelName));
        }
    );

And the revised code to support file-level overrides.

router.route("/app/model/:model")
    .get(
        function (req, res) {
            var model = req.params.model.replace(".js", "");
            if (fs.existsSync(__dirname + req.params.model)) {
                res.send(
                    fs.readFileSync(__dirname + req.params.model, {"encoding": "utf8"})                                                                                      
                );
            } else {
                res.send(router.app.tools.buildExtModel(model));
            }
        }
    );

Leave a Reply

Powered by: Wordpress