Skip to content
eyy edited this page Jan 21, 2014 · 1 revision

Simple

var ReversedWidget = formage.widgets.TextWidget.extend({
    render: function (res) {
        this.value = this.value.split("").reverse().join("");
        this.attrs.style = '-moz-transform: scale(-1, 1); -webkit-transform: scale(-1, 1); transform: scale(-1, 1);';
        this._super(res);
    }
});

var ReversedField = formage.fields.StringField.extend({
    init: function (options) {
        options = options || {};
        options.widget = ReversedWidget;
        this._super(options);
    },
    clean_value: function (req, callback) {
        this.value = this.value.split("").reverse().join("");
        this._super(req, callback);
    }
});

var schema = new mongoose.Schema({
    reversed: { type: String, formageField: ReversedField}
});

Complex

If we want to have a complex underlining type we need to "lie" to mongoose

var TwoDWidget = formage.widgets.TextWidget.extend({
    render: function (res) {
        var value = this.value || {};
        var lat = value.lat;
        var lng = value.lng;
        var name = this.name;
        this.name = name + '_lat';
        this.value = lat;
        this._super(res);
        this.name = name + '_lng';
        this.value = lng;
        this._super(res);
    }
});


var TwoDField = formage.fields.StringField.extend({
    init: function (options) {
        options = options || {};
        options.widget = TwoDWidget;
        this._super(options);
    },
    clean_value: function (req, callback) {
        var lat = Number(req.body[this.name + '_lat']);
        var lng = Number(req.body[this.name + '_lng']);
        this.value = { lat: lat, lng: lng};
        this._super(req, callback);
    }
});
var TwoD = function TwoD(path, options) {
    TwoD.super_.call(this, path, options);
};
util.inherits(TwoD, Schema.Types.Mixed);
Types.TwoD = Object;
Schema.Types.TwoD = TwoD;

var schema = new mongoose.Schema({
    two_d: { type: TwoD, formageField: TwoDField}
});
Clone this wiki locally