Created
August 9, 2012 17:18
-
-
Save particlebanana/3306080 to your computer and use it in GitHub Desktop.
Card Catalog Example
This file contains 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 http = require('http'), | |
setup = require('./setup'), | |
card_catalog = require('cardcatalog'), | |
obj; | |
var setupFn = new setup(); | |
setupFn.on('ready', function(res) { | |
obj = res; | |
}); | |
var requestHandler = function(req, res) { | |
// catalog error handler | |
obj.catalog.on('error', function(error) { | |
res.writeHead(404, {'content-type': 'text/plain'}); | |
res.end('404'); | |
}); | |
// cards error handler | |
obj.catalog.cards.on('error', function(error) { | |
res.writeHead(404, {'content-type': 'text/plain'}); | |
res.end('404'); | |
}); | |
obj.catalog.dispatch(req, res); | |
}; | |
var app = http.createServer(function(req, res) { | |
requestHandler(req, res); | |
}).listen(3000); |
This file contains 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 card_catalog = require('cardcatalog'), | |
util = require('util'); | |
var Example = module.exports = function Example(options) { | |
var self = this; | |
this.name = "Example"; // Required | |
this.slug = "example"; // Required | |
this.ready = false; | |
this.on('error', function(obj) { | |
error_handler.error(obj.res, {status: obj.status}); | |
}); | |
// Create the Example routing table | |
this.router = { | |
'get': { | |
'/': this.index | |
} | |
}; | |
}; | |
util.inherits(Example, card_catalog.card); | |
Example.prototype.index = function text(req, res) { | |
res.writeHead(200, { 'Content-Type': 'application/json' }); | |
res.end('Hello World'); | |
}; |
This file contains 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 card_catalog = require('cardcatalog'), | |
trapper_keeper = require('trapperkeeper'), | |
util = require('util'), | |
events = require('events'); | |
// Setup storage adapter | |
var conn = trapper_keeper.connect('memory'), | |
obj; | |
var setup = module.exports = function setup() { | |
events.EventEmitter.call(); | |
var self = this; | |
conn.connection.on('ready', function() { | |
// Create a 'sites' object and store a 'category' | |
var resource = conn.resource('sites'), | |
record = {"slug" : "test", "plugins" : [ "example" ]}, | |
res; | |
resource.create(record, function(err, result) { | |
res = result; | |
var catalog = new card_catalog.category({ | |
connection: conn, | |
namespace: 'sites' | |
}); | |
catalog.load(); | |
var card = require('./card); | |
var cards = new card_catalog.cards({ | |
cards: [card] | |
}); | |
cards.load(); | |
catalog.cards = cards; | |
obj = { | |
conn: conn, | |
catalog: catalog | |
}; | |
self.emit('ready', obj); | |
}); | |
}); | |
}; | |
util.inherits(setup, events.EventEmitter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment