Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a fairly basic example of usage

  • Loading branch information...
commit f681bf191dcb6bd403be23e2b1420f3048904b25 1 parent 036e80e
@hkjels authored
View
71 crud.js
@@ -21,9 +21,13 @@ var mongoose = require('mongoose')
*/
var form = exports.form = function (model, cb) {
- var err
+ var err = false
+ , form = {}
, Model
, Schema
+ , copy = {
+ 'isRequired': 'required'
+ }
/**
* Retrieve model from mongoose
@@ -49,30 +53,58 @@ var form = exports.form = function (model, cb) {
*/
for (var field in Schema) {
- var fieldtype = field.match(/(number|date|email|password|url|tlf)/gi)
-
- // Required
- Schema[field]['required'] = typeof Schema[field]['isRequired'] !== 'undefined'
- ? Schema[field]['isRequired']
- : null
+ /**
+ * Ignored fields
+ */
- // Field type
- // TODO #crud Add number, date etc
+ for (var prop in Schema[field]) {
+ if (prop === 'selected' && !field[prop]) continue
+ }
- fieldtype = fieldtype || 'text'
- Schema[field]['type'] = typeof Schema[field]['options']['override'] !== 'undefined'
- ? Schema[field]['options']['override']
- : fieldtype
+ form[field] = {}
- // Field name
+ /**
+ * Copy a bunch of parameters
+ *
+ * The value from the copy-object is used as the key to our new form-object
+ * TODO Need a smarter way of pushing values from Schema to form
+ */
- Schema[field]['name'] = field.substring(field.lastIndexOf('.') + 1)
+ for (var key in copy) {
+ if (typeof Schema[field][key] !== 'undefined') {
+ form[field][copy[key]] = Schema[field][key]
+ }
+ }
- for (var prop in Schema[field]) {
- if (prop === 'selected' && !field[prop]) delete Schema[field]
+ /**
+ * Field name
+ */
+
+ form[field]['name'] = field.substring(field.lastIndexOf('.') + 1)
+
+ /**
+ * Field type
+ */
+
+ var fieldtype = field.match(/(number|date|email|password|url|tlf)/gi) || 'text'
+ form[field]['type'] = Schema[field].instance === 'boolean' ? 'checkbox' : fieldtype
+ form[field]['type'] = typeof Schema[field]['options']['override'] !== 'undefined'
+ ? Schema[field]['options']['override']
+ : fieldtype
+ form[field]['type'] = field[0] === '_' ? 'hidden' : fieldtype
+ if (typeof Schema[field]['options']['min'] !== 'undefined'
+ && Schema[field]['options']['min'] >= 50) {
+ form[field]['type'] = 'textarea'
+ }
+ if (typeof Schema[field]['options']['max'] !== 'undefined') {
+ form[field]['max'] = Schema[field]['options']['max']
}
+
+ console.dir(Schema[field])
}
+
+ return cb(err, form)
}
/**
@@ -82,10 +114,11 @@ var form = exports.form = function (model, cb) {
* the specified model.
*/
-exports.middleware = function (req, res, next, model) {
+exports.middleware = function (req, res, next) {
+ var model = req.params.model
form(model, function (err, form) {
if (err) return next(err)
- req.locals('form', form)
+ res.locals({form: form, model: model})
return next()
})
}
View
64 examples/user/app.js
@@ -0,0 +1,64 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express')
+ , app = module.exports = express.createServer()
+ , routes = require('./routes')
+ , mongoose = require('mongoose')
+ , models = require('./models')
+ , stylus = require('stylus')
+ , nib = require('nib')
+ , crud = require('../../crud')
+
+
+/**
+ * Compile stylus with nib
+ */
+
+function compile(str, path) {
+ return stylus(str)
+ .set('filename', path)
+ .set('compress', true)
+ .use(nib())['import']('nib')
+}
+
+/**
+ * Configurations
+ */
+
+app.configure(function(){
+ app.set('views', __dirname + '/views')
+ app.set('view engine', 'jade')
+ app.use(express.bodyParser())
+ app.use(express.methodOverride())
+ app.use(require('stylus').middleware({ src: __dirname + '/public' }))
+ app.use(app.router)
+ app.use(express['static'](__dirname + '/public'))
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
+})
+
+/**
+ * Static helpers
+ */
+
+app.helpers({
+ title: 'Crud user-example'
+ , values: null
+})
+
+/**
+ * Routes
+ */
+
+app.get('/', routes.explain)
+app.get('/:model', crud.middleware, routes.form)
+
+/**
+ * Start listening
+ */
+
+app.listen(3000, function(){
+ console.log('Crud example is listening on port %d', app.address().port)
+})
View
35 examples/user/models/User.js
@@ -0,0 +1,35 @@
+
+/**
+ * Module dependencies
+ */
+
+var mongoose = require('mongoose')
+ , Schema = mongoose.Schema
+
+/**
+ * UserModel
+ *
+ * Will be passed to Crud for dissection. Crud will then normalize and simplify
+ * the JSON-object to something fairly humanfriendly
+ */
+
+var User = new Schema({
+ 'name': {
+ 'first': {type: String, required: true}
+ , 'last': String
+ }
+ , 'email': {
+ 'home': {type: String, match: /[\w]+@[\w]+\.[\w]{2,3}/}
+ , 'work': {type: String, match: /[\w]+@[\w]+\.[\w]{2,3}/}
+ }
+ , 'phone': {type: String, match: /[\d]{8}/, override: 'tlf'}
+ , 'born': Date
+ , 'about': {type: String, min: 80}
+})
+
+/**
+ * Expose User to mongoose
+ */
+
+mongoose.model('User', User)
+
View
3  examples/user/models/index.js
@@ -0,0 +1,3 @@
+
+require('./User')
+
View
46 examples/user/public/stylesheets/form.styl
@@ -0,0 +1,46 @@
+
+form
+ background: white
+ border-radius: 4px
+ box-shadow: 0 1px 5px rgba(black, 0.3)
+ width: 580px
+
+legend
+ background: black
+ border-radius: top 4px
+ color: white
+ display: block
+ font-size: 1.5em
+ padding: 6px 0
+ text-indent: 10px
+ width: 100%
+
+fieldset
+ border: none
+ margin-top: 18px
+
+label
+ display: inline-block
+ text-align: right
+ padding: 0 10px
+ width: 100px
+ &:first-letter
+ text-transform: uppercase
+
+input[type=text]
+ border: none
+ border-radius: 4px
+ box-shadow: inset 0 1px 4px rgba(black, 0.3)
+ font-size: 1em
+ outline: none
+ padding: 0.3em
+ transition: box-shadow .2s ease-in-out
+ &:focus
+ box-shadow: inset 0 1px 4px rgba(black, 0.3), 0 0 5px cyan
+
+input[type=email], input[type=tlf], input[type=url]
+ @extends input[type=text]
+
+textarea
+ @extends input[type=text]
+
View
66 examples/user/public/stylesheets/style.css
@@ -0,0 +1,66 @@
+form {
+ background: #fff;
+ border-radius: 4px;
+ box-shadow: 0 1px 5px rgba(0,0,0,0.3);
+ width: 580px;
+}
+legend {
+ background: #000;
+ border-radius: top 4px;
+ color: #fff;
+ display: block;
+ font-size: 1.5em;
+ padding: 6px 0;
+ text-indent: 10px;
+ width: 100%;
+}
+fieldset {
+ border: none;
+ margin-top: 18px;
+}
+label {
+ display: inline-block;
+ text-align: right;
+ padding: 0 10px;
+ width: 100px;
+}
+label:first-letter {
+ text-transform: uppercase;
+}
+input[type=text],
+input[type=email],
+input[type=tlf],
+input[type=url],
+textarea {
+ border: none;
+ border-radius: 4px;
+ box-shadow: inset 0 1px 4px rgba(0,0,0,0.3);
+ font-size: 1em;
+ outline: none;
+ padding: 0.3em;
+ transition: box-shadow 0.2s ease-in-out;
+}
+input[type=text]:focus,
+input[type=email]:focus,
+input[type=tlf]:focus,
+input[type=url]:focus,
+textarea:focus {
+ box-shadow: inset 0 1px 4px rgba(0,0,0,0.3), 0 0 5px #0ff;
+}
+body {
+ background: #f5f5f5;
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+a {
+ color: #00b7ff;
+}
+.block {
+ display: block;
+}
+.inline {
+ display: inline;
+}
+.inline-block {
+ display: inline-block;
+}
View
16 examples/user/public/stylesheets/style.styl
@@ -0,0 +1,16 @@
+global-reset()
+reset-html5()
+
+@import 'form'
+
+body
+ background: whitesmoke
+ padding: 50px
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif
+a
+ color: #00B7FF
+
+
+for pos in block inline inline-block
+ .{pos}
+ display: pos
View
17 examples/user/routes/index.js
@@ -0,0 +1,17 @@
+
+/**
+ * GET crud form
+ */
+
+exports.form = function(req, res) {
+ res.render('form')
+}
+
+/**
+ * GET explain usage
+ */
+
+exports.explain = function (req, res) {
+ res.render('explain')
+}
+
View
3  examples/user/views/explain.jade
@@ -0,0 +1,3 @@
+
+p You should try to open '/user'
+
View
31 examples/user/views/form.jade
@@ -0,0 +1,31 @@
+
+#wrapper
+ form(action='/api/'+model+'/', method='post')
+ if (values)
+ input(type='hidden', method='put')
+ legend Update
+ else
+ legend New
+
+ //-
+ Loop through every field in the Schema and create the appropriate
+ formelements.
+ @see /docs/crud.md
+
+ fieldset
+ each field in form
+ .inline-block
+ if field.type !== 'hidden'
+ label= field.name
+ if field.type === 'textarea'
+ textarea(name=field.name, required=field.required, maxlength=field.max)
+ else
+ input(type=field.type, name=field.name, required=field.required, maxlength=field.max)
+
+
+
+//- In development-mode, output the Schema to the console
+if (process.env.NODE_ENV !== 'production')
+ script(type='text/javascript')
+ console.dir(!{JSON.stringify(form)})
+
View
6 examples/user/views/layout.jade
@@ -0,0 +1,6 @@
+!!!
+html
+ head
+ title= title
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ body!= body
Please sign in to comment.
Something went wrong with that request. Please try again.