Skip to content

Commit

Permalink
Cleanup/refactor long overdure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson Gariety committed Feb 16, 2014
1 parent 506b470 commit 161b9f1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 67 deletions.
53 changes: 33 additions & 20 deletions lib/sara.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

/**
*
Expand All @@ -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))
Expand Down Expand Up @@ -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.
Expand All @@ -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')
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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))

Expand Down
6 changes: 3 additions & 3 deletions lib/sara/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) {
Expand Down
29 changes: 7 additions & 22 deletions lib/sara/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,36 +64,21 @@ 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()
})
})
}

// 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)

Expand Down
20 changes: 0 additions & 20 deletions lib/sara/options.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/sara/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -147,4 +147,4 @@ var Server = module.exports = (function Server(app) {
})
})
})
})
}

0 comments on commit 161b9f1

Please sign in to comment.