diff --git a/lib/sara.js b/lib/sara.js index 15047fc..b7d5df1 100644 --- a/lib/sara.js +++ b/lib/sara.js @@ -6,15 +6,21 @@ * */ +// Constants +global.IS_SERVER = !process.browser +global.IS_CLIENT = !!process.browser + // Modules var _ = require('./sara/utils') - , Options = require('./sara/options') , jsdom = require('jsdom').jsdom , path = require('path') - -// Constants -global.IS_SERVER = !process.browser -global.IS_CLIENT = !!process.browser + , View = require('./sara/view') + , Model = require('./sara/model') + , Event = require('./sara/event') + , Collection = require('./sara/collection') + , Controller = require('./sara/controller') + , setupServer = require('./sara/server') + , setupClient = require('./sara/client') /** * @@ -25,9 +31,6 @@ global.IS_CLIENT = !!process.browser * */ var Sara = module.exports = (function Sara(options) { - var View = require('./sara/view') - , Model = require('./sara/model') - , Event = require('./sara/event') // Defaults if (IS_SERVER) this.root = path.dirname(_.filepathFromStackIndex(2)) @@ -56,21 +59,16 @@ var Sara = module.exports = (function Sara(options) { // Load options _(this).extend(options) - - // Load CLI arguments - _(this).extend(new Options(process.argv)) }) -// Modules -var Collection = require('./sara/collection') - , Controller = require('./sara/controller') - , Server = require('./sara/server') - , Client = require('./sara/client') - , Local = IS_SERVER ? Server : Client // Include _.method and _.add _.class(Sara) +// Some other fun things +.add('Utils', _) + + /** * Extend the Sara class with an adapter * @param {Object} - adapter - a NodeJS module which exports View and Controller constructors. @@ -83,8 +81,12 @@ _.class(Sara) return this }) -// Some other fun things -.add('Utils', _) +/** + * Load a template and serve it for client-side rendering. + * @param {String} - id - An id to reference the template by. FEMIX: none of this ever + * @param {String} - filepath - Relative path to the template + * @param {Number} - index - A private parameter to get the stack index via. + */ .method(function template(id, filepath, index) { var fs = require('fs') , path = require('path') @@ -112,14 +114,24 @@ _.class(Sara) } } }) + +/** + * Loads a template designed as the app's static layout. + * @param {String} - filepath - Relative path to the layout file. + */ .method(function layout(filepath) { if (IS_SERVER) { this.layout = this.template('layout', filepath, 3) } return this }) + .method('Collection', Collection) .method('Controller', Controller) + +/** + * Define routes to serve on the client via history.pushstate + */ .method(function routes(actions) { for (var route in actions) { this.get(route, actions[route]) @@ -196,7 +208,8 @@ _.class(Sara) setTimeout(function () { fn.bind(this)() - this.local = new Local(this) + if (IS_SERVER) setupServer(this) + else setupClient(this) }.bind(this)) diff --git a/lib/sara/client.js b/lib/sara/client.js index c777744..8c1dafd 100644 --- a/lib/sara/client.js +++ b/lib/sara/client.js @@ -11,7 +11,7 @@ var _ = require('lodash') , domready = require('domready') // Constructor -module.exports = function Client(app) { +module.exports = function setupClient(app) { var engine = require('engine.io-client') app.socket = engine(process.env.port) @@ -36,13 +36,13 @@ module.exports = function Client(app) { } - this.tryInitRouter = function () { + app.tryInitRouter = function () { if (_.every(app.dbstatus)) { app.paths[window.location.pathname]() } } - domready(this.tryInitRouter) + domready(app.tryInitRouter) // A client-side router document.addEventListener('click', function (event) { diff --git a/lib/sara/model.js b/lib/sara/model.js index 8673696..1502da9 100644 --- a/lib/sara/model.js +++ b/lib/sara/model.js @@ -47,7 +47,7 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize , db if (IS_SERVER) adapterServer(Constructor, callback) - else adapterLocalStorage(Constructor, callback) + else if (IS_CLIENT) adapterLocalStorage(Constructor, callback) function callback(datastore) { db = datastore @@ -64,7 +64,7 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize app.dbstatus[name] = true // FIXME: awful hack on next line as well - if (app.local && IS_CLIENT) app.local.tryInitRouter() + if (IS_CLIENT) app.tryInitRouter() }) }) } @@ -72,28 +72,13 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize // JSON endpoints app.get('/' + name.toLowerCase() + '.json', function () { return Constructor.toJSON() - }) - - app.post('/' + name.toLowerCase() + '.json', function () { - - }) - - app.get('/' + name.toLowerCase() + '/:id.json', function () { - - }) - - app.put('/' + name.toLowerCase() + '/:id.json', function () { - - }) - - // app.patch('/' + name.toLowerCase() + '/:id.json', function () { - + }) // .post('/' + name.toLowerCase() + '.json', function () { + // }).get('/' + name.toLowerCase() + '/:id.json', function () { + // }).put('/' + name.toLowerCase() + '/:id.json', function () { + // }).patch('/' + name.toLowerCase() + '/:id.json', function () { + // }).delete('/' + name.toLowerCase() + '/:id.json', function () { // }) - app.delete('/' + name.toLowerCase() + '/:id.json', function () { - - }) - // Include _.method and _.add return _.class(Constructor) diff --git a/lib/sara/options.js b/lib/sara/options.js deleted file mode 100644 index 2807958..0000000 --- a/lib/sara/options.js +++ /dev/null @@ -1,20 +0,0 @@ -/*! - * - * OPTIONS - * - * A simple options parser for Sara. - * - */ - -var Options = module.exports = (function Options(argv) { - argv = argv || process.argv - - for (var i = argv.length; i--;) { - switch (argv[i]) { - case '--env': - case '--environment': - case '-e': - this.env = argv[i + 1] - } - } -}) \ No newline at end of file diff --git a/lib/sara/server.js b/lib/sara/server.js index e724481..985f9b5 100644 --- a/lib/sara/server.js +++ b/lib/sara/server.js @@ -39,7 +39,7 @@ alert = console.log // FIXME: The usefulness of this needs to be debated. * FIXME: Why the fuck is this a constructor? * */ -var Server = module.exports = (function Server(app) { +var Server = module.exports = function setupServer(app) { var engine = require('engine.io') , http = require('http') @@ -147,4 +147,4 @@ var Server = module.exports = (function Server(app) { }) }) }) -}) +}