bin/skeletons/controller/template.ejs
/**
* Autogenerated <%= name %> controller.
*/
var turnpike = require('turnpike');
var util = require('util');
function <%= name %>(connection) {
turnpike.EndpointController.call(this, connection);
}
util.inherits(<%= name %>, turnpike.EndpointController);
module.exports = <%= name %>;
/*
* Your field map function is used to map request body and query fields to
* model fields when their names don't match exactly. In almost all cases,
* with this defined, you won't need to override any of the action methods
* below.
*/
//<%= name %>.prototype.fieldMapper = turnpike.fieldMap({});
//Create an item
/****** Delete this line to override this action ******
<%= name %>.prototype.create = function(readyCallback) {
if (this.model) {
async.waterfall([
function(next) {
this.model.create(next);
}.bind(this),
function(item, next) {
var modelField;
var bodyField;
var fields = this.model.fields();
for (modelField in fields) {
bodyField = this.fieldMapper(modelField, true);
if (this.connection.req.body.hasOwnProperty(bodyField)) {
item[modelField] = this.connection.req.body[bodyField];
}
}
this.model.save(item, next);
}.bind(this)
], function(err, res) {
if (err) {
this.connection.die(500);
}
else {
this.data = res;
readyCallback();
}
}.bind(this));
}
else {
this.connection.die(405);
}
};
//*/
//Edit an existing item
/****** Delete this line to override this action ******
<%= name %>.prototype.edit = function(readyCallback) {
if (this.model){
async.waterfall([
function(next) {
var item = new this.model.Item();
item._id = this.connection.route.params.id;
this.model.load(item, next);
}.bind(this),
function(item, next) {
var modelField;
var bodyField;
var fields = this.model.fields();
if (item) {
for (modelField in fields) {
bodyField = this.fieldMapper(modelField, true);
if (this.connection.req.body.hasOwnProperty(bodyField)) {
item[modelField] = this.connection.req.body[bodyField];
}
}
this.model.save(item, next);
}
else {
next(404);
}
}.bind(this)
], function(err, res) {
if (err === 404) {
this.connection.die(404);
}
else if (err) {
this.connection.die(500);
}
else {
this.data = res;
readyCallback();
}
}.bind(this));
}
else {
this.connection.die(405);
}
};
//*/
//List items
/****** Delete this line to override this action ******
<%= name %>.prototype.index = function(readyCallback) {
if (this.model){
var skip = this.connection.req.query.index;
var params = new this.model.Item();
for (property in params) {
if (this.connection.req.query[property]) {
params[property] = this.connection.req.query[property];
}
}
this.model.index(params, skip, (function(err, data) {
this.data = data;
process.nextTick(readyCallback);
}).bind(this));
}
else {
this.connection.die(405);
}
};
// */
//Display a single item
/****** Delete this line to override this action ******
<%= name %>.prototype.main = function(readyCallback) {
if (this.model) {
var item = new this.model.Item();
item._id = this.connection.route.params.id;
this.model.load(item, (function(err, item){
if (err) {
this.connection.die(500);
}
else if (!item) {
this.connection.die(404);
}
else {
this.data = item;
process.nextTick(readyCallback);
}
}).bind(this));
}
else {
this.connection.die(405);
}
};
// */
//Remove an item
/****** Delete this line to override this action ******
<%= name %>.prototype.remove = function(readyCallback) {
if (this.model){
var item = new this.model.Item();
item._id = this.connection.route.params.id;
this.model.remove(item, function(err, removed) {
this.data.removed = removed;
process.nextTick(readyCallback);
});
}
else {
this.connection.die(405);
}
};
// */
//Form for confirming deletion via web interface
/****** Delete this line to override this action ******
<%= name %>.prototype.removeForm = function(readyCallback) {
if (this.model) {
var item = new this.model.Item();
item._id = this.connection.route.params.id;
this.model.load(item, function(err, item){
if (err) {
this.connection.die(500);
}
else if (!item) {
this.connection.die(404);
}
else {
if (Session.csrf.state) {
item._csrf = this.connection.req.csrfToken();
}
this.data = item;
process.nextTick(readyCallback);
}
}.bind(this));
}
else {
this.connection.die(405);
}
};
// */
//Form for entering new items and editing existing ones
/****** Delete this line to override this action ******
<%= name %>.prototype.editForm = function(readyCallback) {
var item;
if (this.model) {
item = new this.model.Item();
async.waterfall([
function(next) {
if (this.connection.route.params.id) {
item._id = this.connection.route.params.id;
this.model.load(item, next);
}
else {
next(false, item);
}
}.bind(this),
function(item, next) {
if (this.connection.route.params.id) {
//check if item came back empty, send a 404 if it did
if (!item) {
next(404);
}
else {
next(false, item);
}
}
else {
next(false, item);
}
}.bind(this)
],
function(err, item){
var modelField;
var bodyField;
if (err === 404) {
this.connection.die(404);
}
else if (err) {
this.connection.die(500);
}
else {
if (Session.csrf.state) {
this.data._csrf = this.connection.req.csrfToken();
}
for (modelField in item) {
if (item.hasOwnProperty(modelField)) {
bodyField = this.fieldMapper(modelField);
this.data[bodyField] = item[modelField];
}
}
process.nextTick(readyCallback);
}
}.bind(this));
}
else {
this.connection.die(405);
}
};
//*/