jackson
Version:
Jackson, the web application framework
215 lines (186 loc) • 6.89 kB
JavaScript
// Generated by CoffeeScript 1.9.3
(function() {
var Controller, addClassHelpers, async, clone, extend, path, ref, snakeCase,
slice = [].slice,
hasProp = {}.hasOwnProperty;
path = require('path');
async = require('async');
snakeCase = require('snake-case');
ref = require('./util'), extend = ref.extend, clone = ref.clone, addClassHelpers = ref.addClassHelpers;
Controller = (function() {
addClassHelpers(Controller);
Controller.bind = function() {
var callbacks, event;
event = arguments[0], callbacks = 2 <= arguments.length ? slice.call(arguments, 1) : [];
this._callbacks = clone(this._callbacks);
return this._callbacks[event] = (this._callbacks[event] || []).concat(callbacks);
};
Controller.fire = function() {
var cb, events, i, instance;
instance = arguments[0], events = 3 <= arguments.length ? slice.call(arguments, 1, i = arguments.length - 1) : (i = 1, []), cb = arguments[i++];
if (typeof cb !== 'function') {
events.push(cb);
cb = null;
}
return async.forEachSeries(events, this._fire.bind(this, instance), cb);
};
Controller._fire = function(instance, event, cb) {
var callbacks, iter, ref1;
callbacks = (ref1 = this._callbacks) != null ? ref1[event] : void 0;
if (!(callbacks != null ? callbacks.length : void 0)) {
return typeof cb === "function" ? cb() : void 0;
}
iter = function(callback, cb) {
var error;
if (typeof callback === 'string') {
callback = instance[callback];
}
try {
if (callback.length === 1) {
return instance.apply(callback, cb);
} else {
return cb(instance.apply(callback) === true);
}
} catch (_error) {
error = _error;
instance.error(error);
return cb(true);
}
};
return async.forEachSeries(callbacks, iter, cb);
};
Controller.before = function() {
var action, callbacks;
action = arguments[0], callbacks = 2 <= arguments.length ? slice.call(arguments, 1) : [];
return this.bind.apply(this, ["before:" + action].concat(slice.call(callbacks)));
};
Controller.beforeAll = function() {
var callbacks;
callbacks = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return this.bind.apply(this, ['before'].concat(slice.call(callbacks)));
};
Controller.after = function() {
var action, callbacks;
action = arguments[0], callbacks = 2 <= arguments.length ? slice.call(arguments, 1) : [];
return this.bind.apply(this, ["after:" + action].concat(slice.call(callbacks)));
};
Controller.afterAll = function() {
var callbacks;
callbacks = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return this.bind.apply(this, ['after'].concat(slice.call(callbacks)));
};
Controller.helper = function(name, fn) {
this.helpers = clone(this.helpers);
return this.helpers[name] = fn;
};
Controller.prototype.status = 200;
function Controller(app, req, res, route) {
var base;
this.app = app;
this.req = req;
this.res = res;
this.route = route != null ? route : {};
this.view = {
route: this.route
};
this.headers = {};
(base = this.route).params || (base.params = {});
this.name || (this.name = this.constructor.name);
if (typeof this.initialize === "function") {
this.initialize();
}
}
Controller.prototype.header = function(name, value, replace) {
if (replace == null) {
replace = true;
}
if (replace === false && this.headers[name.toLowerCase()]) {
return;
}
return this.headers[name.toLowerCase()] = value;
};
Controller.prototype.viewContext = function(context) {
return clone(this.app.constructor.helpers, this.constructor.helpers, this.view, context);
};
Controller.prototype.render = function(tpl, context) {
var body;
tpl = path.join(this.templateDir || '', tpl);
body = this.app.renderTemplate(this.templateRoot, tpl, this.viewContext(context));
return this.respond(body);
};
Controller.prototype.respond = function(status, body) {
var ref1;
if (!body) {
ref1 = [status, this.status], body = ref1[0], status = ref1[1];
}
if (typeof body === 'object') {
this.header('content-type', 'application/json', false);
body = JSON.stringify(body);
} else if (typeof body === 'string') {
this.header('content-type', 'text/html', false);
}
this.header('content-length', body.length, false);
this.res.writeHead(status, this.headers);
return this.res.end(body);
};
Controller.prototype.apply = function() {
var action, args, error, fn, templateName;
fn = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
if (typeof fn === 'string') {
action = this[fn];
if (typeof action === 'string') {
templateName = action;
this.render(templateName);
return;
} else {
fn = action;
}
}
try {
return fn.apply(this, args);
} catch (_error) {
error = _error;
this.error(error);
return true;
}
};
Controller.prototype.applyAsAction = function(action) {
var key, value;
return this.apply.apply(this, [action].concat(slice.call((function() {
var ref1, results;
ref1 = this.route.params;
results = [];
for (key in ref1) {
if (!hasProp.call(ref1, key)) continue;
value = ref1[key];
if (key !== '_') {
results.push(value);
}
}
return results;
}).call(this)), slice.call(this.route.params._ || [])));
};
Controller.prototype.callAction = function(action, cb) {
return this.constructor.fire(this, 'before', (function(_this) {
return function() {
return _this.constructor.fire(_this, "before:" + action, function() {
_this.applyAsAction(action);
return _this.constructor.fire(_this, 'after', function() {
return _this.constructor.fire(_this, "after:" + action, cb);
});
});
};
})(this));
};
Controller.prototype.error = function(error) {
return this.app.render(this.req, this.res, 'error', error);
};
Controller.prototype.log = function() {
var msgs, ref1;
msgs = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return (ref1 = this.app).log.apply(ref1, [this.name.yellow, "|"].concat(slice.call(msgs)));
};
return Controller;
})();
module.exports = Controller;
}).call(this);