Permalink
Browse files

Overall structure now works

Overall structure now works
  • Loading branch information...
1 parent 652b474 commit 9331ef3fcc6aab150f0c4c04e7ae6c5a7a0c90bf @ewoudj committed Sep 18, 2011
Showing with 118 additions and 56 deletions.
  1. +22 −22 index.js
  2. +25 −10 private/server/http.js
  3. +25 −5 private/server/middleware.js
  4. +34 −13 public/controls/application.js
  5. +9 −6 public/controls/topbar.js
  6. +3 −0 public/models/user.js
View
@@ -1,24 +1,24 @@
-var exports = module.exports = function(){
-};
-
-exports.version = '0.1.0';
-exports.control = require('./public/controls/control').control;
-exports.utils = require('./public/controls/control').utils;
-exports.page = require('./public/controls/page');
-exports.topbar = require('./public/controls/topbar');
-exports.rest = require('./public/net/rest');
-exports.list = require('./public/controls/list').list;
-exports.form = require('./public/controls/form').form;
-exports.model = require('./public/data/model');
-exports.store = require('./public/data/store').store;
-exports.textcontrol = require('./public/controls/formtextcontrol').control;
-exports.memoControl = require('./public/controls/formmemocontrol').control;
-exports.referenceControl = require('./public/controls/formreferencecontrol').control;
-exports.referenceListControl = require('./public/controls/formreferencelistcontrol').control;
-exports.radioControl = require('./public/controls/formradiocontrol').control;
-exports.application = require('./public/controls/application').application;
-exports.httpserver = require('./private/server/http');
+var onServer = (typeof window === 'undefined');
-exports.models = {
- user: require('./public/models/user.js')
+exports = module.exports = {
+ version: '0.1.0',
+ control: require('./public/controls/control').control,
+ utils: require('./public/controls/control').utils,
+ page: require('./public/controls/page'),
+ topbar: require('./public/controls/topbar'),
+ rest: require('./public/net/rest'),
+ list: require('./public/controls/list').list,
+ form: require('./public/controls/form').form,
+ model: require('./public/data/model'),
+ store: require('./public/data/store').store,
+ textcontrol: require('./public/controls/formtextcontrol').control,
+ memoControl: require('./public/controls/formmemocontrol').control,
+ referenceControl: require('./public/controls/formreferencecontrol').control,
+ referenceListControl: require('./public/controls/formreferencelistcontrol').control,
+ radioControl: require('./public/controls/formradiocontrol').control,
+ application: require('./public/controls/application').application,
+ httpserver: onServer ? require('./private/server/http') : null,
+ models: {
+ user: require('./public/models/user.js')
+ }
};
View
@@ -5,19 +5,34 @@ var middleware = require('./middleware');
var framework = require('./../..');
exports = module.exports = {
- createServer : function(port, models){
+ createServer : function(port, config){
+ var requires = [__dirname + '/../../index.js'];
+ if(!config){
+ config = {};
+ }
+ // The available models are typically specified by the application using the framework.
+ // The defening application send the list of models in the config for the createServer function.
+ if(config.models){
+ for(var i = 0; i < config.models.length; i++){
+ // Add to the requires list: this makes the model available on the client
+ requires.push(config.models[i]);
+ // Require the model: this makes the model register with the model registry
+ require(arguments.callee.caller.arguments[4] /*__dirname from caller*/ + config.models[i].substring(1));
+ }
+ }
+ middleware.config = config;
var result = connect(
form({ keepExtensions: true }),
connect.static(__dirname + '/../../static', { maxAge: 0 }),
-// browserify({
-// ignore : [
-// __dirname + '/../../private/data/storage.js',
-// __dirname + '/../../private/server/http.js',
-// __dirname + '/../../private/server/middleware.js',
-// 'mongodb', 'crypto', 'md5'],
-// require : __dirname + '/../../index.js',
-// mount : '/script.js'
-// }),
+ browserify({
+ ignore : [
+ './private/data/storage',
+ './private/server/http',
+ './private/server/middleware',
+ 'mongodb', 'crypto', 'md5'],
+ require : requires,
+ mount : '/script.js'
+ }),
connect.query(),
connect.cookieParser(),
connect.session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}),
@@ -1,6 +1,11 @@
+var merge = require('./../../public/controls/control').utils.merge;
var application = require('./../../public/controls/application').application;
+var model = require('./../../public/data/model');
+var store = require('./../../public/data/store').store;
+var user = require('./../../public/models/user');
-exports = module.exports = {
+var middleware = exports = module.exports = {
+ config: {},
/*
* Waits for upload of form to finish than continues
*/
@@ -90,7 +95,7 @@ exports = module.exports = {
*/
handleFormPost: function(req, res, next){
if(req.method === 'POST'){
- model.registry[data._type].set({value: data, session: session}, function(err, setResult){
+ model.registry[req.body._type].set({value: req.body, session: req.session}, function(err, setResult){
if(setResult && setResult.issues){
req.query = setResult;
}
@@ -122,12 +127,27 @@ exports = module.exports = {
var app = new application(req.query, req.session);
var applicationClass = "/index.js";
app.ready(function() {
+ req.query.models = middleware.config.models;
var clientConfig = JSON.stringify(req.query);
var clientData = JSON.stringify(app.store.clientCache);
var sessionData = JSON.stringify({user: req.session ? req.session.user : null});
- var script = "function boot(){\nvar app = new (require('" + applicationClass + "').application)(";
- script = script + clientConfig + ", " + sessionData + " ," + clientData + ");\napp.bind(document.childNodes[0]);\n}";
- app.head.bootScript.controlValue = script;
+ var s = "function boot(){\n";
+ var s = s + " var framework = require('/index.js');\n";
+ var s = s + " require.modules['frameworkjs'] = function(){ return framework; }\n";
+ var s = s + " var app = framework.application;\n";
+ var s = s + " var config = " + clientConfig + ";\n";
+ var s = s + " if(config.models){\n";
+ var s = s + " for(var i = 0; i < config.models.length; i++){\n";
+ var s = s + " var m = config.models[i];\n";
+ var s = s + " m = m.substring(m.lastIndexOf('/'));\n";
+ var s = s + " require(m);\n";
+ var s = s + " }\n";
+ var s = s + " }\n";
+ var s = s + " ";
+ var s = s + " var application = new app( config, " + sessionData + " ," + clientData + ");\n";
+ var s = s + " application.bind(document.childNodes[0]);\n";
+ var s = s + "}";
+ app.head.bootScript.controlValue = s;
var html = app.render();
res.writeHead(200, {
'Content-Length': html.length,
@@ -3,6 +3,9 @@ var control = require('./control').control;
var page = require('./page').page;
var model = require('./../data/model');
var store = require('./../data/store').store;
+var registry = model.registry;
+var rest = require('./../net/rest').utils;
+var user = require('./../models/user');
/*
* The application class is used to start
*
@@ -30,7 +33,7 @@ var application = function(config, session, clientData){
viewItem = {
controlType: 'list',
name: 'searchList',
- query: {_type:'user'},
+ query: {_type: config.model || 'user'},
setSearch: function(query){
alert(query);
}
@@ -79,6 +82,33 @@ var application = function(config, session, clientData){
* If we have a valid viewItem, make sure we construct it now to trigger any async
* loading that calls busy() and done()
*/
+ var navigationItems = [];
+ var currentModel = null;
+ for(var s in registry){
+ var registeredModel = registry[s];
+ if(!currentModel || (config.query && config.query.model === registeredModel.name)){
+ currentModel = registeredModel;
+ }
+ if(registeredModel.navigatable && registeredModel.canUserNavigate(session.user)){
+ navigationItems.push({
+ tag:'a',
+ controlValue: registeredModel.navigationTitle || registeredModel.friendlyName,
+ attributes: {
+ cls: 'sidepanelbutton',
+ href: rest.toUrl({
+ view: 'search',
+ model: registeredModel.name,
+ query: ""
+ })
+ }
+ });
+ }
+ };
+ navigationItems.push({
+ tag: 'footer',
+ name: 'mainfooter',
+ controlValue: config.copyright || '2011(c) All rights reserved'
+ });
page.call(this, {
title: config.title || 'Default title',
store: new store({clientCache: clientData}),
@@ -90,7 +120,8 @@ var application = function(config, session, clientData){
controlType: 'topbar',
name: 'topbar',
user: session ? session.user : null,
- title: application.config.title
+ title: application.config.title,
+ model: currentModel
},{
name: 'navigationcontrol',
attributes: {
@@ -99,17 +130,7 @@ var application = function(config, session, clientData){
items: [ {
tag: 'nav',
name: 'sidepanel',
- items:[{
- tag:'a',
- controlValue: 'Users',
- attributes: {
- cls: 'sidepanelbutton'
- }
- },{
- tag: 'footer',
- name: 'mainfooter',
- controlValue: config.copyright || '2011(c) All rights reserved'
- }]
+ items:navigationItems
}, {
tag: 'section',
name: 'viewStack',
View
@@ -48,10 +48,14 @@ var topbar = function(config){
}); // "index.html?view=search&query=" + this.parentControl.searchInput.element.value ;
}
}
- },{
+ }]
+ });
+ merge(this, config);
+ if(this.model){
+ this.items.push({
tag: 'a',
name: 'createTopicButton',
- controlValue: 'Create Topic',
+ controlValue: 'Create ' + this.model.name,
attributes:{
cls: 'button'
},
@@ -60,13 +64,12 @@ var topbar = function(config){
window.location.href = rest.toUrl({
view: 'form',
mode: 'edit',
- model: 'topic'
+ model: this.model.name
}); // "index.html?view=form&mode=new&model=topic";
}
}
- }]
- });
- merge(this, config);
+ });
+ }
control.call(this);
};
View
@@ -8,6 +8,9 @@ var userModel = new model({
friendlyName: 'user',
titleField: 'userName',
submitTitle: 'Submit',
+ navigationTitle: 'Users',
+ navigatable: true,
+ canUserNavigate: function(){return true;},
permissions: {
createRoles: ['?'], // Everybody can create an account
readRoles: ['?'],

0 comments on commit 9331ef3

Please sign in to comment.