Skip to content

Commit

Permalink
update to get it working with latest version (node, mongoose, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
stunti committed Feb 13, 2011
1 parent b8224f0 commit 900851b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 29 deletions.
5 changes: 3 additions & 2 deletions app.js
Expand Up @@ -3,16 +3,17 @@ require("./lib/underscore");
var Server = {},
express = require("express"),
path = require("path"),
sys = require("sys"),
application_root = __dirname;

global.Server = Server;
Server.root = application_root;
global.app = express.createServer();

Server.setup = require("./lib/setup.js").setup({
redis: require("./lib/redis-client").createClient(),
//redis: require("./lib/redis-client").createClient(),
app: app,
mongoose : require("mongoose").Mongoose,
mongoose : require("mongoose"),
io : require("socket.io"),
express : express,
paths : {
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/index_controller.js
@@ -0,0 +1,14 @@
var mongoose = require("mongoose");
var actions = {};
user = mongoose.model('User');
actions.index = function(request,response){
user.find({}, function(err, docs){
//sys.puts(sys.inspect(users));
response.render('index',{
results : docs,
total : docs.length
})
});
};

module.exports = actions;
28 changes: 11 additions & 17 deletions app/models/user.js
@@ -1,17 +1,11 @@
module.exports.define = function(){
return {
properties: ['first_name', 'last_name', 'email', 'updated_at'],
indexes: ['id'],
getters: {
full_name: function(){
return this.first_name + ' ' + this.last_name;
}
},
methods: {
save: function(fn){
this.updated_at = new Date();
this.__super__(fn);
}
}
};
}
var mongoose = require("mongoose");
var sys = require("sys");
var schema = new mongoose.Schema({
name : { type: String, default: 'hahaha' }
, age : { type: Number, min: 18, index: true }
, bio : { type: String, match: /[a-z]/ }
, date : { type: Date, default: Date.now }
});

mongoose.model('User', schema);

4 changes: 3 additions & 1 deletion lib/controllers.js
Expand Up @@ -5,15 +5,17 @@ module.exports = Server.controllers = {
Server.controllers.autoload = function(db){
var fs = require("fs"),
path = require("path"),
sys = require("sys"),
files = fs.readdirSync( Server.paths.controllers ),
util = require("./util"),
names = _.map(files,function(f){
return( path.basename(f) );
});


_.each(names,function(controller){
var c_id = util.camelize( controller.replace(/.js$/,'') );
Server.controllers.controller_objects[c_id] =
require( Server.paths.controllers + "/" + controller );
});
};
};
10 changes: 8 additions & 2 deletions lib/models.js
Expand Up @@ -3,12 +3,18 @@ module.exports = Server.models = {};
Server.models.autoload = function(db){
var fs = require("fs"),
path = require("path"),
mongoose = require("mongoose"),
sys = require("sys"),
files = fs.readdirSync( Server.paths.models ),
names = _.map(files,function(f){
return( path.basename(f) );
});
//Model = mongoose.noSchema('test', db);
//mongoose.load(Server.paths.models);


_.each(names,function(model){
require( Server.paths.models + "/" + model ).define();
require( Server.paths.models + "/" + model );
});
};
sys.puts(sys.inspect(Server.models));
};
13 changes: 11 additions & 2 deletions lib/routes.js
Expand Up @@ -9,14 +9,23 @@ var match = function(url,handler,method){
util = require("./util"),
controller = parts.shift(),
action = parts.shift(),
sys = require('sys'),
method = method || "get";

//sys.puts(sys.inspect(controller));

if(!controller.match(/_controller$/)){
controller = controller + "_controller";
if (controller == 'site') {
controller = 'Index';
}
controller = controller + "Controller";
}

sys.puts(sys.inspect(Server.controllers.controller_objects));
sys.puts(sys.inspect(controller));

var controller_id = util.camelize(controller),
action_handler = Server.controllers.controller_objects[controller_id][ action ];
action_handler = Server.controllers.controller_objects[controller][ action ];

//add the handler for the url
if(url && action_handler){
Expand Down
8 changes: 3 additions & 5 deletions lib/setup.js
Expand Up @@ -3,7 +3,7 @@
module.exports.setup = function(o){
var sys = require("sys"),
app = o.app,
redis = o.redis,
// redis = o.redis,
socket = o.app.socket,
mongoose = o.mongoose,
io = o.io,
Expand All @@ -17,8 +17,6 @@ module.exports.setup = function(o){
require("./controllers.js").autoload(app);
require("./routes.js").draw(app);

console.log( sys.inspect(Server.controllers) );

app.configure(function(){
app.set('view engine','haml');
app.set('views', o.paths.views);
Expand All @@ -31,7 +29,7 @@ module.exports.setup = function(o){

app.listen(o.port || 3000);
app.socket = io.listen(app);

/*
// redis pub/sub with socket.io
redis.subscribeTo("socket-io-broadcast:*",function(channel,message,pattern){
var payload = JSON.stringify({
Expand All @@ -41,6 +39,6 @@ module.exports.setup = function(o){
app.socket.broadcast(payload);
});

*/

};

0 comments on commit 900851b

Please sign in to comment.