Skip to content

Commit

Permalink
Added more documentation. Adding in form helper functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed May 9, 2010
1 parent eb1d2bf commit c509a4c
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 19 deletions.
9 changes: 4 additions & 5 deletions app/controllers/benjamin-coe.js
@@ -1,11 +1,10 @@
exports.BenjaminCoe = MobiusController.extend({
index: function() {
var self = this;
MobiusModel.BlogPost.create({
title : 'Testing out the framework I have been building.',
body : 'Test blog post body.',
date : new Date()
});


this.params['date'] = new Date();
MobiusModel.BlogPost.create(this.params['BlogPost']);

MobiusModel.BlogPost.find(
{
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/blog.js
Expand Up @@ -2,5 +2,9 @@ exports.Blog = MobiusController.extend({
index: function() {
this.title = "Blog";
this.generatedFile = "app/controllers/blog.js";
},

create: function() {

}
});
6 changes: 3 additions & 3 deletions app/models/blog-post.js
@@ -1,5 +1,5 @@
exports.BlogPost = MobiusModel.extend({
});
exports.BlogPost.date = {index: true};
exports.BlogPost.title = {index: true};
exports.BlogPost.body = {};
exports.BlogPost.date = {index: true, type: 'date'};
exports.BlogPost.title = {index: true, type: 'text'};
exports.BlogPost.body = {type: 'textarea'};
7 changes: 4 additions & 3 deletions app/views/benjamin-coe.html.mejs
@@ -1,5 +1,6 @@
<h1><%= post.title %></h1>
Date: <i><%= post.date %></i>
<h1><%= post ? post.title : '' %></h1>
Date: <i><%= post ? post.date : '' %></i>
<p>
<%=h( post.body )%>
<%=h( post ? post.body : '' )%>
<%=form( MobiusModel.BlogPost )%>
</p>
15 changes: 14 additions & 1 deletion app/views/layout.html.mejs
@@ -1 +1,14 @@
<%= body %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Mobius-JS</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-darkness/jquery-ui.css" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script src="public/javascript/mobius-client.js"></script>
</head>
<body>
<%= body %>
</body>
</html>
7 changes: 7 additions & 0 deletions config/routes.json
Expand Up @@ -6,6 +6,13 @@
"action" : "index"
},

{
"route" : "",
"method" : "post",
"controller" : "benjamin-coe",
"action" : "index"
},

{
"route" : "/blog",
"method" : "get",
Expand Down
4 changes: 2 additions & 2 deletions lib/mobius-js/bootstrap.js
Expand Up @@ -135,7 +135,7 @@ Bootstrap.prototype.createIndexes = function(models, mobiusProcessingStack) {

var modelClassName = helpers.stringToClassName(modelKey, '-');
models[modelKey].prototype['className'] = modelClassName;
var modelInstance = new models[modelKey](mobiusProcessingStack);
var modelInstance = new models[modelKey](mobiusProcessingStack, models[modelKey]);

var indexes = ['meta', ['_id', 1]];

Expand Down Expand Up @@ -169,7 +169,7 @@ exports.createIndexes = Bootstrap.prototype.createIndexes;
*/
Bootstrap.prototype.initializeControllers = function(controllers, models, mobiusProcessingStack) {
for (var modelKey in models) {
var modelInstance = new models[modelKey](mobiusProcessingStack);
var modelInstance = new models[modelKey](mobiusProcessingStack, models[modelKey]);
var modelClassName = helpers.stringToClassName(modelKey, '-');
for (var controllerKey in controllers) {
MobiusModel[modelClassName] = modelInstance;
Expand Down
3 changes: 2 additions & 1 deletion lib/mobius-js/controller.js
Expand Up @@ -24,6 +24,7 @@
var resig = require('resig/resig');
var sys = require('sys');
var helpers = require('mobius-js/helpers/helpers');
var webHelpers = require('mobius-js/helpers/web-helpers')

/**
* A Mobius controller inherits from a Resig class.
Expand Down Expand Up @@ -155,7 +156,7 @@ MobiusController.prototype._loadParams = function() {
if (this.express.params.get && this.express.params.post) {
// Merge the get and post parameters.
for (var key in this.express.params.post) {
this.express.params.get[key] = express.params.post[key];
this.express.params.get[key] = this.express.params.post[key];
}
this.params = this.express.params.get;
} else
Expand Down
2 changes: 1 addition & 1 deletion lib/mobius-js/db/mongodb.js
Expand Up @@ -62,7 +62,7 @@ process.EventEmitter.prototype.emit = function (type) {
// slower
var args = Array.prototype.slice.call(arguments, 1);
// My ugly monkey patch.
if (args[0]['ok']) {
if (args[0] && args[0]['ok']) {
args[0] = null; // My monkey patch.
}
this._events[type].apply(this, args);
Expand Down
40 changes: 39 additions & 1 deletion lib/mobius-js/helpers/web-helpers.js
Expand Up @@ -17,4 +17,42 @@
* Benjamin Coe (BenjaminCoe.com) - MIT Licensed.
*
* Description: Web related helper functions.
*/
*/

// Dependencies.
var sys = require('sys');

// This class is created for documentation purposes.
function WebHelpers() {};

/**
* Creates a form from a mobius model.
*
*/
WebHelpers.prototype.form = function(model) {
var returnMe = '<form method="post" class="' + model.className + '">'

for (var key in model.constructor) {
if (key != 'constructor' && key != 'extend' && key != 'include') {
switch (model.constructor[key]['type']) {
case 'date':
inputClass = 'datepicker';
returnMe += '<input type="text" name="' + model.className + '[' + key + ']" class="datepicker" /><br />';
break;

case 'textarea':
returnMe += '<textarea name="' + model.className + '[' + key + ']"/></textarea><br />';
break;

default:
returnMe += '<input type="text" name="' + model.className + '[' + key + ']"/><br />';
break;
}
}
}

return returnMe += '<input type="submit" value="Save" /></form>';
}
exports.form = WebHelpers.prototype.form;

exports.parseParameters = WebHelpers.prototype.parseParameters;
33 changes: 31 additions & 2 deletions lib/mobius-js/model.js
Expand Up @@ -36,13 +36,29 @@ MobiusModel = resig.Class.extend( {
* MongoDB provides the persistent storage. Note: Do not access this method
* directly, instead use MobiusModel.extend({});
*
* @param {object} processingStack synchronous processing stack.
* @param {class} constructor constructor which can be used to create new instance of this class.
* @type void
* @public
*/
MobiusModel.prototype.init = function(processingStack) {
MobiusModel.prototype.init = function(processingStack, constructor) {
this.processingStack = processingStack;
this.constructor = constructor;
},

/**
* Map parameters onto a new instance of this class.
*
* @param {object} params key value pairs of instance variables.
* @type void
* @public
*/
MobiusModel.prototype.loadParameters = function(params) {
for (var key in params) {
this[key] = params[key];
}
}

/**
* Close the database connection.
*
Expand Down Expand Up @@ -101,6 +117,19 @@ MobiusModel.prototype.find = function(query, sort, callback) {
params['sort']['sort'].push(temp);
}

// Add the class name into the parameters.
params['collectionName'] = this.className;
this.processingStack.find(params, callback);

// We need to intercept the results and map them onto
// an instance of this class.
var self = this;
this.processingStack.find(params, function(results) {
temp = [];
for (var key in results) {
var c = new self.constructor(self.processingStack, self.constructor);
c.loadParameters(results[key]);
temp.push(c);
}
callback(temp);
});
}
3 changes: 3 additions & 0 deletions lib/mobius-js/renderer/mejs.js
Expand Up @@ -23,9 +23,12 @@
// Dependencies.
var sys = require('sys');
var utils = require('express/utils');
var webHelpers = require('mobius-js/helpers/web-helpers');


// Helper functions available in templates.
var h = utils.escape;
var form = webHelpers.form;

/**
* EJS Developer 1.0 Production
Expand Down

0 comments on commit c509a4c

Please sign in to comment.