Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Squashed bug

Used a global variable for Model that was overwritten when there was
more than one model
  • Loading branch information...
commit c9b18bcac77f9c3c5dc1a0f1b56fbefbfc49ea99 1 parent 966b805
@hkjels authored
View
2  examples/user/views/explain.jade
@@ -5,7 +5,7 @@ table
thead
tr
th Method
- th Adress
+ th Address
th Response
tbody
tr
View
6 examples/user/views/form.jade
@@ -1,10 +1,10 @@
-form(action='/api/'+form.model+'/'+(form.id || ''), method='post')
+form(action='/api/'+form.name+'/'+(form.id || ''), method='post')
if (form.filled)
input(type='hidden', name='_method', value='put')
- legend= 'Update '+form.name
+ legend= 'Update'
else
- legend= 'New '+form.name
+ legend= 'New'
//-
Loop through every field in the Schema and create the appropriate
View
249 lib/crud.js
@@ -20,131 +20,150 @@ var express = require('express')
: express.HTTPServer.prototype
/**
- * Model & instance-name
+ * Resource
+ *
+ * @param {mongoose.Model} Model
*/
-var instance
- , Model
+var Resource = function Resource (Model) {
+ this.Model = Model
+}
/**
- * Monkeypatch express with `crud`
- */
+ * Index GET /
+ *
+ * Will display a list of all the instances the model can find
+ */
-app.crud = function (name, options) {
- for (var model in mongoose.models) {
- if (!mongoose.models.hasOwnProperty(model)) continue
- if (model.toLowerCase() === name.toLowerCase()) Model = mongoose.model(model)
+Resource.prototype.index = function (req, res) {
+ this.Model.find({}, function (err, docs) {
+ res.json(docs)
+ res.end()
+ })
+}
+
+/**
+ * New GET /new
+ *
+ * Displays a form for creating new instances
+ */
+
+Resource.prototype.new = function (req, res) {
+ var form = friendly(this.Model)
+ form.name = Model.modelName.toLowerCase()
+ res.render('form', {'form': form})
+}
+
+/**
+ * Create POST /
+ *
+ * Creates a new instance in the database, based on the result of `new`
+ */
+
+Resource.prototype.create = function (req, res, next) {
+ var model = new this.Model()
+
+ for (var key in req.body) {
+ if (key === '_id' || !req.body[key]) continue
+ model[key] = req.body[key]
}
- instance = en.singularize(Model.modelName).toLowerCase()
- this.resource(name, resource)
+
+ model.save(function (err) {
+ if (err) return next(err)
+ res.redirect('back')
+ })
}
/**
- * Resourcefull routes
+ * Show GET /:instance
+ *
+ * Show information about a single instance
*/
-var resource = {
-
- /**
- * Index GET /
- *
- * Will display a list of all the instances the model can find
- */
-
- index: function (req, res) {
- Model.find({}, function (err, docs) {
- res.json(docs)
- res.end()
- })
- }
-
- /**
- * New GET /new
- *
- * Displays a form for creating new instances
- */
-
- , 'new': function (req, res) {
- var form = friendly(Model)
-
- form.name = instance.toLowerCase()
- form.model = Model.modelName.toLowerCase()
-
- res.render('form', {'form': form})
- }
-
- /**
- * Create POST /
- *
- * Creates a new instance in the database, based on the result of `new`
- */
-
- , create: function (req, res, next) {
- var model = new Model()
-
- for (var key in req.body) {
- if (key == '_id' || !req.body[key]) continue
- model[key] = req.body[key]
- }
-
- model.save(function (err) {
- if (err) return next(err)
- res.redirect('back')
- })
- }
-
- /**
- * Show GET /:instance
- *
- * Show information about a single instance
- */
-
- , show: function (req, res) {
- var instance = instance.toLowerCase()
- res.json(req[instance])
- res.end()
- }
-
- , edit: function (req, res) {
- var instance = instance.toLowerCase()
- , form = friendly(req[instance])
-
- form.name = instance
- form.model = Model.modelName.toLowerCase()
-
- res.render('form', {'form': form})
- }
-
- , update: function (req, res) {
- var instance = instance.toLowerCase()
- , model = req[instance]
-
- for (var key in req.body) {
- if (key === '_id') continue
- model[key] = req.body[key]
- }
-
- model.save(function (err) {
- if (err) throw err
- res.redirect('back')
- })
- }
-
- , destroy: function (req, res, next) {
- var instance = instance.toLowerCase()
- , model = req[instance]
-
- instance.remove()
- instance.save(function (err) {
- if (err) return next(err)
- res.redirect('back')
- })
- }
-
- , load: function (req, id, cb) {
- Model.findById(id, function (err, doc) {
- cb(err, doc)
- })
- }
+Resource.prototype.show = function (req, res) {
+ res.json(req[this.Model.modelName])
+ res.end()
+}
+
+/**
+ * Edit
+ *
+ * Display a form for editing existing instance
+ */
+
+Resource.prototype.edit = function (req, res) {
+ var instance = en.singularize(this.Model.modelName).toLowerCase()
+ , model = req[instance]
+ , form = friendly(model)
+ form.name = Model.modelName.toLowerCase()
+ res.render('form', {'form': form})
+}
+
+/**
+ * Update
+ *
+ * Update document with changes passed to req.body
+ */
+
+Resource.prototype.update = function (req, res) {
+ var instance = en.singularize(this.Model.modelName).toLowerCase()
+ , model = req[instance]
+
+ for (var key in req.body) {
+ if (key === '_id') continue
+ model[key] = req.body[key]
+ }
+
+ model.save(function (err) {
+ if (err) throw err
+ res.redirect('back')
+ })
+}
+
+/**
+ * Destroy
+ *
+ * Remove record from document
+ */
+
+Resource.prototype.destroy = function (req, res, next) {
+ var instance = en.singularize(this.Model.modelName).toLowerCase()
+ , model = req[instance]
+
+ model.remove()
+ model.save(function (err) {
+ if (err) return next(err)
+ res.redirect('back')
+ })
+}
+
+/**
+ * Load
+ *
+ * Autoload record using the passed id
+ */
+
+Resource.prototype.load = function (req, id, cb) {
+ this.Model.findById(id, function (err, doc) {
+ cb(err, doc)
+ })
+}
+
+/**
+* Monkeypatch express with `crud`
+*/
+
+app.crud = function (name, actions, opts) {
+ var options = actions || {}
+ , res
+
+ for (var model in mongoose.models) {
+ if (!mongoose.models.hasOwnProperty(model)) continue
+ if (model.toLowerCase() === name.toLowerCase()) Model = mongoose.model(model)
+ }
+
+ this.resources = this.resources || {}
+ res = this.resources[name] = this.resource(name, new Resource(Model))
+ return res
}
Please sign in to comment.
Something went wrong with that request. Please try again.