xanax
Version:
ExpressJS REST adapter for Spine models
335 lines (312 loc) • 10.9 kB
JavaScript
// Generated by IcedCoffeeScript 108.0.9
var Xanax, errify, express, extend, iced, util, __iced_k, __iced_k_noop,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__slice = [].slice,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
iced = require('iced-runtime');
__iced_k = __iced_k_noop = function() {};
util = require("util");
express = require("express");
errify = require("errify");
extend = util._extend;
Xanax = (function() {
function Xanax(_arg) {
var fn, path, _ref;
this.Model = _arg.Model, this.name = _arg.name, this.router = _arg.router;
this["delete"] = __bind(this["delete"], this);
this.patch = __bind(this.patch, this);
this.update = __bind(this.update, this);
this.edit = __bind(this.edit, this);
this.read = __bind(this.read, this);
this.find = __bind(this.find, this);
this["new"] = __bind(this["new"], this);
this.create = __bind(this.create, this);
this.index = __bind(this.index, this);
this.respond = __bind(this.respond, this);
this.name || (this.name = "" + (this.Model.className.toLowerCase()) + "s");
this.router || (this.router = new express.Router);
this.router.get("/" + this.name, this.index);
this.router.post("/" + this.name, this.create);
this.router.get("/" + this.name + "/new", this["new"]);
this.router.get("/" + this.name + "/:id", this.find, this.read);
this.router.get("/" + this.name + "/:id/edit", this.find, this.edit);
this.router.put("/" + this.name + "/:id", this.find, this.update);
this.router.patch("/" + this.name + "/:id", this.find, this.patch);
this.router["delete"]("/" + this.name + "/:id", this.find, this["delete"]);
this.paths = {};
_ref = this.constructor.paths;
for (path in _ref) {
fn = _ref[path];
this.paths[path] = fn.call(this);
}
}
Xanax.paths = {
index: function() {
return "" + this.name + "/index";
},
create: function() {
return "" + this.name + "/create";
},
"new": function() {
return "" + this.name + "/new";
},
show: function() {
return "" + this.name + "/show";
},
edit: function() {
return "" + this.name + "/edit";
},
update: function() {
return "" + this.name + "/update";
},
patch: function() {
return "" + this.name + "/patch";
},
"delete": function() {
return "" + this.name + "/delete";
}
};
Xanax.prototype.respond = function() {
var path, record, records, res, response;
res = arguments[0], path = arguments[1], records = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
if (records.length === 1 && path !== this.paths.index) {
record = records[0];
response = record.attributes();
} else if (records.length) {
response = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = records.length; _i < _len; _i++) {
record = records[_i];
_results.push(record.attributes());
}
return _results;
})();
}
return this.render(res, path, response);
};
Xanax.prototype.render = function(res, path, response) {
return res.render(path, response);
};
Xanax.prototype.index = function(req, res, next) {
var ideally, key, query, records, value, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
ideally = errify(next);
query = req.query;
key = value = null;
(function(_this) {
return (function() {
for (key in query) {
value = query[key];
if (__indexOf.call(_this.Model.attributes, key) >= 0) {
return delete query[key];
}
}
});
})(this)();
(function(_this) {
return (function(__iced_k) {
if (key) {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/xanax/src/xanax.coffee",
funcname: "Xanax.index"
});
_this.Model.findAllByAttribute(key, value, query, ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return records = arguments[0];
};
})(),
lineno: 53
})));
__iced_deferrals._fulfill();
})(__iced_k);
} else {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/xanax/src/xanax.coffee",
funcname: "Xanax.index"
});
_this.Model.findAll(query, ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return records = arguments[0];
};
})(),
lineno: 55
})));
__iced_deferrals._fulfill();
})(__iced_k);
}
});
})(this)((function(_this) {
return function() {
return _this.respond.apply(_this, [res, _this.paths.index].concat(__slice.call(records)));
};
})(this));
};
Xanax.prototype.create = function(req, res, next) {
var ideally, record, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
ideally = errify(next);
record = new this.Model(req.body);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/xanax/src/xanax.coffee",
funcname: "Xanax.create"
});
record.save(ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return record = arguments[0];
};
})(),
lineno: 62
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
return _this.respond(res, _this.paths.create, record);
};
})(this));
};
Xanax.prototype["new"] = function(req, res, next) {
return this.respond(res, this.paths["new"], new this.Model);
};
Xanax.prototype.find = function(req, res, next) {
var ideally, record, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
ideally = errify(next);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/xanax/src/xanax.coffee",
funcname: "Xanax.find"
});
_this.Model.find(req.params.id, ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return record = arguments[0];
};
})(),
lineno: 70
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
res.locals.record = record;
return next();
};
})(this));
};
Xanax.prototype.read = function(req, res, next) {
return this.respond(res, this.paths.show, res.locals.record);
};
Xanax.prototype.edit = function(req, res, next) {
return this.respond(res, this.paths.edit, res.locals.record);
};
Xanax.prototype.update = function(req, res, next) {
var ideally, record, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
ideally = errify(next);
record = res.locals.record;
delete req.body.id;
record.load(req.body);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/xanax/src/xanax.coffee",
funcname: "Xanax.update"
});
record.save(ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return record = arguments[0];
};
})(),
lineno: 85
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
return _this.respond(res, _this.paths.update, record);
};
})(this));
};
Xanax.prototype.patch = function(req, res, next) {
var ideally, record, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
ideally = errify(next);
record = res.locals.record;
extend(record, req.body);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/xanax/src/xanax.coffee",
funcname: "Xanax.patch"
});
record.save(ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return record = arguments[0];
};
})(),
lineno: 92
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
return _this.respond(res, _this.paths.patch, record);
};
})(this));
};
Xanax.prototype["delete"] = function(req, res, next) {
var ideally, record, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
ideally = errify(next);
record = res.locals.record;
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/xanax/src/xanax.coffee",
funcname: "Xanax.delete"
});
record.remove(ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return record = arguments[0];
};
})(),
lineno: 98
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
return _this.respond(res, _this.paths["delete"]);
};
})(this));
};
return Xanax;
})();
module.exports = Xanax;
//# sourceMappingURL=xanax.js.map