Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit c45d6498e79b7ef5e26fef2361ce5cb508832106 @radicaldesigns committed Sep 13, 2010
Showing with 191 additions and 0 deletions.
  1. +12 −0 .gitmodules
  2. +35 −0 ajax.js
  3. +13 −0 app.js
  4. 0 client/index.html
  5. +1 −0 client/say
  6. +10 −0 controllers.js
  7. +77 −0 controllers/example.js.txt
  8. +16 −0 models.js
  9. +24 −0 models/example.js.txt
  10. +1 −0 vendor/connect
  11. +1 −0 vendor/mongoose
  12. +1 −0 vendor/node-utils
@@ -0,0 +1,12 @@
+[submodule "vendor/node-utils"]
+ path = vendor/node-utils
+ url = git@github.com:radicaldesigns/node-utils.git
+[submodule "vendor/mongoose"]
+ path = vendor/mongoose
+ url = git@github.com:radicaldesigns/mongoose.git
+[submodule "vendor/connect"]
+ path = vendor/connect
+ url = http://github.com/senchalabs/connect.git
+[submodule "client/say"]
+ path = client/say
+ url = git@github.com:radicaldesigns/say.git
@@ -0,0 +1,35 @@
+var sys = require('sys');
+
+// load models and controllers
+var models = require('./models');
+var controllers = require('./controllers');
+
+exports.router = function(app){
+ app.post('/api/ajax/v1', function(req, res, next) {
+ sys.log('AJAX REQUEST: '+sys.inspect(req.body));
+
+ // validate the request
+ var validated = true;
+ if(!req.body.app_name) {
+ sys.log('AJAX ERROR: req.body.app_name not present');
+ validated = false;
+ }
+ if(!req.body.event) {
+ sys.log('AJAX ERROR: req.body.event not present');
+ validated = false;
+ }
+
+ // try to run the action on the controller
+ if(validated) {
+ if(controllers[req.body.app_name]) {
+ if(controllers[req.body.app_name][req.body.event]) {
+ controllers[req.body.app_name][req.body.event](req, res, models);
+ } else {
+ sys.log('AJAX ERROR: controller "'+req.body.app_name+'" does not have action "'+req.body.event+'"');
+ }
+ } else {
+ sys.log('AJAX ERROR: controller "'+req.body.app_name+'" does not exist');
+ }
+ }
+ });
+};
@@ -0,0 +1,13 @@
+var sys = require('sys');
+var connect = require('./vendor/connect/lib/connect');
+
+// start the web server
+var server = connect.createServer(
+ connect.logger({ format: ':method :url' }),
+ connect.bodyDecoder(),
+ connect.staticProvider(__dirname + '/client'),
+ connect.router(require('./ajax').router)
+);
+server.listen(8080);
+sys.log("Listening on port 8080");
+
No changes.
Submodule say added at 9cb3fc
@@ -0,0 +1,10 @@
+var fs = require('fs');
+var controllers = {};
+controller_dir = fs.readdirSync('./controllers');
+for(i in controller_dir) {
+ var split = controller_dir[i].split('.');
+ if(split[1] == 'js') {
+ var controller_name = split[0];
+ exports[controller_name] = require('./controllers/'+controller_name);
+ }
+}
@@ -0,0 +1,77 @@
+var sys = require('sys');
+var async = require('../vendor/async');
+
+exports.initialize = function(req, res, models) {
+ var folder_results, status_results, email_results;
+
+ function process_data(data) {
+ for(i in data) {
+ data[i]._id = data[i]._id.toHexString();
+ }
+ return data;
+ }
+
+ async.auto({
+ folder: function(callback){
+ models.Folder.find({}, false).all(function(data){
+ folder_results = process_data(data);
+ callback(null, 'folder');
+ });
+ },
+ status: function(callback){
+ models.Status.find({}, false).all(function(data){
+ status_results = process_data(data);
+ callback(null, 'status');
+ });
+ },
+ email: function(callback){
+ models.Email.find({}, false).all(function(data){
+ email_results = process_data(data);
+ callback(null, 'Email');
+ });
+ },
+ respond: ['folder', 'status', 'email', function(callback) {
+ var response = {
+ model: {
+ Email: { update: email_results },
+ Folder: { update: folder_results },
+ Status: { update: status_results },
+ }
+ };
+ //sys.puts(sys.inspect(response, true, null));
+ res.writeHead(200);
+ res.end(JSON.stringify(response));
+
+ callback(null, 'responded');
+ }]
+ });
+};
+
+exports.save_email = function(req, res, models){
+ var data = JSON.parse(req.body.data);
+
+ if(data._id == "") {
+ sys.puts('email.save_email: no id, so creating a new row');
+ // if there's a blank _id, create it
+ email = new models.Email({
+ sender: data.sender,
+ subject: data.subject,
+ body: data.body,
+ folder__id: data.folder__id,
+ status__id: data.status__id
+ });
+ email.save();
+ } else {
+ sys.puts('email.save_email: updating an existing row');
+ // otherwise update it
+ models.Email.update(data._id, {
+ sender: data.sender,
+ subject: data.subject,
+ body: data.body,
+ folder__id: data.folder__id,
+ status__id: data.status__id
+ }, function(email){
+ sys.puts('EMAIL: '+sys.inspect(email));
+ });
+ }
+};
@@ -0,0 +1,16 @@
+var fs = require('fs');
+var util = require('./vendor/node-utils/utils');
+var mongoose = require('./vendor/mongoose/mongoose').Mongoose;
+var models = {};
+var db = mongoose.connect('mongodb://localhost/templator');
+model_dir = fs.readdirSync('./models');
+for(i in model_dir) {
+ var split = model_dir[i].split('.');
+ if(split[1] == 'js') {
+ var model_file = split[0];
+ var model_name = util.class_case(model_file);
+ mongoose.model(model_name, require('./models/'+model_file)[model_name]);
+ exports[model_name] = db.model(model_name);
+ }
+}
+
@@ -0,0 +1,24 @@
+exports.Email = {
+ properties: [
+ 'sender',
+ 'subject',
+ 'body',
+ 'folder__id',
+ 'status__id'
+ ],
+
+ indexes: ['sender', 'subject', 'folder__id', 'status__id'],
+
+ // class methods
+ static: {},
+
+ // instance methods
+ methods: {},
+
+ // custom setters
+ settings: {},
+
+ // custom getters
+ getters: {}
+}
+
Submodule connect added at 1e81c0
Submodule mongoose added at 994742
Submodule node-utils added at 9ea5f5

0 comments on commit c45d649

Please sign in to comment.