Skip to content

Commit

Permalink
refactored bones:
Browse files Browse the repository at this point in the history
* the assets directory of each module will be available at /assets/modulename
* each module is required to have a package.json
* the core router is no longer loaded automatically. you have to add it yourself in the server file
* views no longer have access to models
* renamed bones.Bones to Bones.utils
* plugins are loaded in an additive fashion; they now have access to all previously loaded plugins
* /bones.*.js is no located at /assets/bones/*.js
* renamed confusingly named "options" param to "app"
* require('bones').plugin now contains the set of plugins loaded
  • Loading branch information
kkaefer committed May 2, 2011
1 parent eee71c4 commit 17b4038
Show file tree
Hide file tree
Showing 39 changed files with 329 additions and 321 deletions.
File renamed without changes.
42 changes: 42 additions & 0 deletions bones.js
@@ -0,0 +1,42 @@
module.exports = {
$: require('jquery'),
_: require('underscore'),
express: require('express'),
mirror: require('mirror'),

utils: require('bones/server/utils'),

Backbone: require('bones/server/backbone'),
Controller: require('bones/server/controller'),
Model: require('bones/server/model'),
Collection: require('bones/server/collection'),
Router: require('bones/server/router'),
View: require('bones/server/view'),
Server: require('bones/server/server'),
Command: require('bones/server/command'),

get plugin() {
if (!global.__BonesPlugin__) {
var Plugin = require('./server/plugin');
global.__BonesPlugin__ = new Plugin();
require('./core');
}
return global.__BonesPlugin__;
},

load: function(dir) {
this.plugin.directories.push(dir);
this.plugin
.require(dir, 'controllers')
.require(dir, 'models')
.require(dir, 'routers')
.require(dir, 'templates')
.require(dir, 'views')
.require(dir, 'servers')
.require(dir, 'commands');
},

start: function() {
return this.plugin.start();
}
};
4 changes: 2 additions & 2 deletions client/core.js → client/utils.js
Expand Up @@ -13,7 +13,7 @@ $(function() {
var model = callback(models);
models[model.title] = model;
} else if (kind === 'view') {
var view = callback(models, views, templates);
var view = callback(views, templates);
views[view.title] = view;
} else if (kind === 'controller') {
var controller = callback(models, views, controllers);
Expand All @@ -32,7 +32,7 @@ $(function() {
}

Backbone.history.start();
}
};

Bones.DEBUG = {
models: models,
Expand Down
2 changes: 1 addition & 1 deletion client/view.prefix.js
@@ -1,4 +1,4 @@
// ---- start __NAME__ view ----

Bones.initialize('view', function(models, views, templates) {
Bones.initialize('view', function(views, templates) {
var view;
23 changes: 23 additions & 0 deletions commands/start.bones
@@ -0,0 +1,23 @@
command = Bones.Command.extend();

command.description = 'start application';

command.options.adminParty = {
'shortcut': 'a',
'description': 'Enable admin mode.',
'default': false
};

command.prototype.initialize = function(plugin) {
if (!Object.keys(plugin.servers).length) {
console.warn(Bones.utils.colorize('No servers defined.', 'red'));
return;
}

this.servers = {};
for (var server in plugin.servers) {
this.servers[server] = new plugin.servers[server](plugin);
this.servers[server].start();
console.warn('Started %s.', Bones.utils.colorize(this.servers[server], 'green'));
}
};
21 changes: 21 additions & 0 deletions core.js
@@ -0,0 +1,21 @@
var _ = require('underscore');
var path = require('path');
var fs = require('fs');
var bones = require('bones');

require('bones').load(__dirname);

require.extensions['._'] = function(module, filename) {
var content = fs.readFileSync(filename, 'utf8');
var name = bones.utils.camelize(path.basename(filename).replace(/\..+$/, ''));

module.exports = _.template(content);
module.exports.register = function(app) {
if (app.assets) {
app.assets.templates.push({
filename: filename,
content: 'template = ' + module.exports + ';'
});
}
};
};
23 changes: 0 additions & 23 deletions core/commands/start.bones

This file was deleted.

20 changes: 0 additions & 20 deletions core/index.js

This file was deleted.

6 changes: 2 additions & 4 deletions examples/simple/index.js
@@ -1,8 +1,6 @@
#!/usr/bin/env node
var plugin = module.exports = require('../..').plugin(__dirname);

plugin.load();
require('bones').load(__dirname);

if (!module.parent) {
plugin.start();
require('bones').start();
}
4 changes: 4 additions & 0 deletions examples/simple/package.json
@@ -0,0 +1,4 @@
{
"name": "simple",
"version": "1.0.0"
}
4 changes: 2 additions & 2 deletions examples/simple/servers/main.bones
Expand Up @@ -2,6 +2,8 @@ var mirror = require('mirror');

server = Bones.Server.extend({
initialize: function() {
this.register(routers['Core']);

this.register(models['Foo']);
this.register(views['Home']);
this.register(views['Foo']);
Expand All @@ -10,7 +12,5 @@ server = Bones.Server.extend({
this.register(controllers['Default']);

this.register(templates['Home']);

this.server.use(middleware.txmtErrorHandler);
}
});
19 changes: 0 additions & 19 deletions index.js

This file was deleted.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -2,6 +2,8 @@
"name": "bones",
"version": "1.0.0",

"main": "./bones.js",

"dependencies": {
"underscore": "= 1.1.5",
"express": "= 2.2.2",
Expand Down
62 changes: 34 additions & 28 deletions core/routers/core.bones → routers/core.bones
@@ -1,20 +1,23 @@
var fs = require('fs');
var path = require('path');

router = Bones.Router.extend({});

router.prototype.initialize = function(options) {
this.initializeMiddleware(options);
this.initializeStatic(options);
this.initializeAssets(options);
this.initializeModels(options);
this.initializeCollections(options);
router.prototype.initialize = function(app) {
this.initializeMiddleware(app);
this.initializeStatic(app);
this.initializeAssets(app);
this.initializeModels(app);
this.initializeCollections(app);
};

router.prototype.initializeMiddleware = function(options) {
router.prototype.initializeMiddleware = function(app) {
this.server.use(express.bodyParser());
this.server.use(express.cookieParser());
};

router.prototype.initializeModels = function(options) {
this.models = options.models;
router.prototype.initializeModels = function(app) {
this.models = app.models;

// Bind model routes to server.
_.bindAll(this, 'loadModel', 'getModel', 'saveModel', 'delModel');
Expand All @@ -24,26 +27,29 @@ router.prototype.initializeModels = function(options) {
this.server.del('/api/:model/:id', this.loadModel, this.delModel);
};

router.prototype.initializeCollections = function(options) {
this.models = options.models;
router.prototype.initializeCollections = function(app) {
this.models = app.models;

this.server.get('/api/:collection', this.loadCollection.bind(this));
};

router.prototype.initializeStatic = function(options) {
this.server.use(express['static'](options.plugin.directory + '/client'));
router.prototype.initializeStatic = function(app) {
app.plugin.directories.forEach(function(dir) {
var pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
app.server.use('/assets/' + pkg.name, express['static'](path.join(dir, 'assets')));
});
};

router.prototype.initializeAssets = function(options) {
options.assets = {
router.prototype.initializeAssets = function(app) {
app.assets = {
vendor: [
'bones/core/assets/jquery',
'bones/assets/jquery',
'underscore',
'backbone'
],
core: [
'bones/shared/core',
'bones/client/core',
'bones/shared/utils',
'bones/client/utils',
'bones/shared/backbone',
'bones/client/backbone'
],
Expand All @@ -53,24 +59,24 @@ router.prototype.initializeAssets = function(options) {
templates: []
};

var assets = options.assets;
var assets = app.assets;
var wrapper = {
wrapper: Bones.wrapClientFile
wrapper: Bones.utils.wrapClientFile
};

this.server.get('/bones.vendor.js', mirror.assets(require, assets.vendor));
this.server.get('/bones.core.js', mirror.assets(require, assets.core));
this.server.get('/assets/bones/vendor.js', mirror.assets(require, assets.vendor));
this.server.get('/assets/bones/core.js', mirror.assets(require, assets.core));

this.server.get('/bones.controllers.js', mirror.assets(require, assets.controllers, wrapper));
this.server.get('/bones.models.js', mirror.assets(require, assets.models, wrapper));
this.server.get('/bones.views.js', mirror.assets(require, assets.views, wrapper));
this.server.get('/bones.templates.js', mirror.source(assets.templates, wrapper));
this.server.get('/assets/bones/controllers.js', mirror.assets(require, assets.controllers, wrapper));
this.server.get('/assets/bones/models.js', mirror.assets(require, assets.models, wrapper));
this.server.get('/assets/bones/views.js', mirror.assets(require, assets.views, wrapper));
this.server.get('/assets/bones/templates.js', mirror.source(assets.templates, wrapper));
};

var headers = { 'Content-Type': 'application/json' };

router.prototype.loadCollection = function(req, res, next) {
var name = Bones.camelize(Bones.pluralize(req.params.collection));
var name = Bones.utils.camelize(Bones.utils.pluralize(req.params.collection));
if (name in this.models) {
// Pass any querystring paramaters to the collection.
req.collection = new this.models[name]([], req.query);
Expand All @@ -88,7 +94,7 @@ router.prototype.loadCollection = function(req, res, next) {
};

router.prototype.loadModel = function(req, res, next) {
var name = Bones.camelize(req.params.model);
var name = Bones.utils.camelize(req.params.model);
if (name in this.models) {
req.model = new this.models[name]({ id: req.params.id });
}
Expand Down
8 changes: 4 additions & 4 deletions server/collection.js
Expand Up @@ -7,15 +7,15 @@ Backbone.Collection.toString = function() {
return '<Collection ' + this.title + '>';
};

Backbone.Collection.register = function(server) {
Backbone.Collection.register = function(app) {
// Add the controller if it's not a server-only controller.
this.files.forEach(function(filename) {
if (!(/\.server\.bones$/).test(filename)) {
server.assets.models.push(filename);
if (!(/\.server\.bones$/).test(filename) && app.assets) {
app.assets.models.push(filename);
}
});

server.models[this.title] = this;
app.models[this.title] = this;
};

Backbone.Collection.prototype.toString = function() {
Expand Down
6 changes: 3 additions & 3 deletions server/command.js
Expand Up @@ -3,12 +3,12 @@ var _ = require('underscore');

module.exports = Command;

function Command(options) {
function Command(app) {
this.options = Object.create(Command.options);
this.initialize(options);
this.initialize(app);
};

Command.prototype.initialize = function() {};
Command.prototype.initialize = function(app) {};

Command.augment = Backbone.Controller.augment;
Command.extend = Backbone.Controller.extend;
Expand Down

0 comments on commit 17b4038

Please sign in to comment.