Permalink
Browse files

sorted out the session problems, cleaning up

  • Loading branch information...
rayui committed Feb 3, 2012
1 parent 850b343 commit f7cefd4cc689238e9ed9725d5f50b0c1218eb3ce
Showing with 41 additions and 31 deletions.
  1. +1 −1 js/modules/db.js
  2. +9 −3 js/modules/model.js
  3. +5 −5 js/modules/models.js
  4. +20 −14 js/modules/server.js
  5. +4 −7 js/modules/sessions.js
  6. +2 −1 templates/index.jade
View
@@ -44,7 +44,7 @@ Database.prototype = Object.create(events.EventEmitter.prototype, {
Database.prototype.query = function(schemaName, query, fields, callback) {
var schema = new mongoose.Schema(schemas[schemaName].definition);
var model = this.db.model(schemaName, schema);
- callback.call(callback, model);
+ var results = model.find(query, fields, callback);
};
exports.Database = Database;
View
@@ -15,10 +15,11 @@ var Model = function(name) {
//mix in model actions
_.extend(this, models[name]);
events.EventEmitter.call(this);
-
return this;
};
+var onReady =
+
Model.super = events.EventEmitter;
Model.prototype = Object.create(events.EventEmitter.prototype, {
constructor: {
@@ -27,14 +28,19 @@ Model.prototype = Object.create(events.EventEmitter.prototype, {
}
});
-Model.prototype.ready = function(callback) {
+Model.prototype.onReady = function(callback) {
+ this.removeAllListeners('model:ready');
this.on('model:ready', function(data) {
+ //send back an empty model if null result
callback.call(callback, data);
});
};
Model.prototype.query = function(query, fields, callback) {
- this.emit('db:query', query, fields, callback);
+ this.emit('db:query', this.schema.name, query, fields, function(data) {
+ data = data || {};
+ callback.call(this, data);
+ });
};
Model.prototype.end = function(data) {
View
@@ -28,12 +28,12 @@ models.User = {
get: function(body) {
var that = this;
- /*this.query({}, [], function(_data) {
- console.log(_data);
- that.end(that.data);
- });*/
+ this.query({}, [], function(_data) {
+ //clone it! do not take a reference
+ that.data = _.extend({},_data);
+ that.end(_data);
+ });
- this.end(this.data);
},
post:function(body) {
this.data.id = body.id;
View
@@ -8,7 +8,7 @@ var _ = require('underscore')._,
sessions = require('./sessions');
//set up server model
-var Server = function(_settings){
+var Server = function(_settings){
//default settings
var settings = {
options:{
@@ -52,7 +52,6 @@ var Server = function(_settings){
//renders a chunk of markup to the response object
function serveHTML(data, template, callback) {
- data.settings = settings
jade.renderFile(__dirname + template, data, function(err,html) {
if (err) {
data.err = err;
@@ -80,14 +79,18 @@ var Server = function(_settings){
var headers = route.headers(req.headers, req.params);
function sendJSON(data) {
- res.json(data, 200);
+ res.json(data, 200);
};
function sendHTML(data) {
var template = settings.options['template_dir'] + '/' + route['template'] + '.jade';
serveHTML(data, template, utilities.callback(sendResponse, {args:[headers,res]}));
};
+ function queryDB(schemaName, query, fields, callback) {
+ that.emit('db:query', schemaName, query, fields, callback);
+ };
+
switch (route.type) {
case '302':
res.redirect(headers['Location']);
@@ -102,23 +105,26 @@ var Server = function(_settings){
break;
case 'dynamic':
default:
- var sessionId = req.session.sessionId;
+ var sessionId = req.session.id,
+ session = sessionHandler.getSession(sessionId);
//get or create a session
- if (!sessionId) {
- sessionId = sessionHandler.createSession();
- req.session.sessionId = sessionId;
+ if (!session) {
+ session = sessionHandler.createSession(sessionId);
}
- //get or create the model for this route
- if (!sessionHandler.getSession(sessionId)[route.model]) {
- m = sessionHandler.createModel(req.session.sessionId, route.model);
- m.on('db:query', function(query, fields, _callback) {
- that.emit('db:query', m.schema.name, query, fields, _callback);
- });
+ var m = session.models[route.model];
+
+ if (!m) {
+ //get or create the model for this route
+ m = sessionHandler.createModel(sessionId, route.model);
+ m.on('db:query', queryDB);
}
- headers['Content-Type'] === 'application/json' ? m.ready(sendJSON) : m.ready(sendHTML);
+ //bind response callbacks based on request type
+ headers['Content-Type'] === 'application/json' ? m.onReady(sendJSON) : m.onReady(sendHTML);
+
+ //get the model ready
m[req.method.toString().toLowerCase()](req.body);
break;
View
@@ -29,16 +29,12 @@ Handler.prototype = Object.create(events.EventEmitter.prototype, {
}
});
-Handler.prototype.createSession = function() {
+Handler.prototype.createSession = function(sessionId) {
//creates new session object in sessions
//returns object
- var md5sum = new crypto.createHash('md5');
- md5sum.update(new Date().getTime().toString());
-
- var sessionId = md5sum.digest('base64');
sessions[sessionId] = {models:{}};
- return sessionId;
+ return sessions[sessionId];
};
Handler.prototype.destroySession = function(sessionId) {
@@ -55,7 +51,8 @@ Handler.prototype.getSession = function(sessionId) {
Handler.prototype.createModel = function(sessionId, modelName) {
var that = this;
- var m = sessions[sessionId]['models'][modelName] = new model.Model(modelName);
+ var m = new model.Model(modelName);
+ sessions[sessionId]['models'][modelName] = m;
return m;
};
View
@@ -10,7 +10,8 @@ body
label(for="name") Name
input#name(type="text", name="name")
input(type="submit")
- #model #{id}: #{name}
+ if(typeof id !== 'undefined' && typeof name !== 'undefined')
+ #model #{id}: #{name}
#errors
a(href="/config") View the config

0 comments on commit f7cefd4

Please sign in to comment.