Created
October 15, 2012 15:21
-
-
Save dallonf/3893059 to your computer and use it in GitHub Desktop.
Route Event resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Resource = require('deployd/lib/resource') | |
| , Script = require('deployd/lib/script') | |
| , util = require('util'); | |
| function RouteEvent() { | |
| Resource.apply(this, arguments); | |
| } | |
| util.inherits(RouteEvent, Resource); | |
| RouteEvent.label = "Route Event"; | |
| RouteEvent.events = ["get", "post"]; | |
| module.exports = RouteEvent; | |
| RouteEvent.prototype.clientGeneration = true; | |
| RouteEvent.prototype.handle = function (ctx, next) { | |
| var parts = ctx.url.split('/').filter(function(p) { return p; }); | |
| var result = {}; | |
| var domain = { | |
| url: ctx.url | |
| , parts: parts | |
| , query: ctx.query | |
| , body: ctx.body | |
| , 'this': result | |
| , setResult: function(val) { | |
| result = val; | |
| } | |
| }; | |
| if (ctx.method === "POST" && this.events.post) { | |
| this.events.post.run(ctx, domain, function(err) { | |
| ctx.done(err, result); | |
| }); | |
| } else if (ctx.method === "GET" && this.events.get) { | |
| this.events.get.run(ctx, domain, function(err) { | |
| ctx.done(err, domain.result); | |
| }); | |
| } else { | |
| next(); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment