Skip to content

Outing Formage Form

eyy edited this page Feb 3, 2014 · 2 revisions

How to use a formage form from the outside?

var formageForm = function(name, req_key, options) {
    var model = registry.admin.models[name];
    return function(req, res, next) {
        var Form = model.options.form,
            document = req[req_key],
            opts = _.assign(opts || {}, model.options),
            data = req.method == 'POST' ? _.merge({}, req.body, req.files) : {};

        var form = req.form = new Form(opts, model.model, document, data);
        form.pre_process().then(function() {
            res.locals.form = form.toString();
            res.locals.head = form.render_head();
            next();
        });
    };
};

app.get('/settings', formageForm('users', 'user'), function(req, res) {
    res.render('settings', {
        title: 'Settings'
    });
});
app.post('/settings/account', formageForm('users', 'user'), function(req, res) {
    req.form.save().then(
        function() {
            res.render('message', {
                title: 'Account Settings',
                message: 'Your changes were saved.'
            });
        },
        function(err) {
            console.log(err);
            res.render('settings', {
                title: 'Account Settings'
            });
        }
    );
});
Clone this wiki locally