GithubHelp home page GithubHelp logo

nvdnkpr / formage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from node4good/formage

0.0 3.0 0.0 5.08 MB

Bootstraped Admin Module for MongoDB via Mongoose

Home Page: formage.io

License: MIT License

formage's Introduction

Formage Build Status

Bootstraped Admin Forms for MongoDB via Mongoose, originally forked from mongoose-admin.

Usage

npm install formage

var express = require('express'),
    app = express();

require('formage').init(app, express [, models, options]);

Look at \example directory.

Options

// Site-wide options, and their default values
require('formage').init(app, express, models, {
    title: 'Admin',
    root: '/admin',
    default_section: 'main',
    username: 'admin',
    password: 'admin',
    admin_users_gui: ture
);

Model options

var model = new mongoose.model('songs', schema);

model.label = 'My Songs';
model.singular = 'Song';

// external files specific to this model
model.static = {
   js: [ '/js/songs.js' ],
   css: ['/css/songs.css' ]
};

model.formage = {
    // one-document models
    is_single: true,

    filters: ['artist', 'year'],

    // additional actions on this model
    actions: [
       {
          value: 'release',
          label: 'Release',
          func: function (user, ids, callback) {
             console.log('You just released songs ' + ids);
             callback();
          }
       }
    ],

    // list of fields to be displayed by formage for this model
    list: ['number', 'title', 'album', 'artist', 'year'],

    // list of order fields
    order_by: ['-year', 'album', 'number'],

    // list of fields that must be populated
    // (see http://mongoosejs.com/docs/api.html#document_Document-populate)
    list_populate: ['album'],

    // list of fields on which full-text search is available
    search: ['title', 'album', 'artist']
};

Field options

var schema = new mongoose.Schema({
    artist: { type: String, label: 'Who made it?' },
    // lang is a two letter ISO 639-1 code as recognized by google
    location: { type: Schema.Types.GeoPoint, widget_options: {lang: 'nl'}}
});

ISO 639-1

Extending

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}
});

Shout-out to my man @jrowny


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);
    },
    to_schema: function () {
        return {
            lat: Number,
            lng: Number
        };
    },
    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}
});

License

MIT

Sponsor

formage's People

Contributors

ishai avatar laurentvb avatar alonronin avatar eyy avatar moul avatar

Watchers

Navid Nikpour avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.