Faraday
A lightweight MVC framework.
To begin, run the following the in the root of your app:
coffee node_modules/faraday/new.coffee
Now you can access faraday throught the folders models
, views
, and controllers
.
The Server
To run the server, run
./develop
If you get this:
-bash: ./develop: Permission denied
Run this:
chmod +x develop
This gives develop executable permission. To get develop to work, you may need to install the ruby gem called sass.
Models and Modooses
I have abandoned the old models
folder in favor of the modooses
model. You can still use models
, but they are deprecated and will be removed in subsequent versions.
A modoose is a humorous malamoneau of "model" and "mongoose". (I can hear you laughing). Each modoose corresponds to a mongoose model in a coffee file in the modooses folder. To create a modoose called Foo, save the file ./modooses/Foo.cofffee
:
fooSchema = # a mongoose schema for Foo...
module.exports = require('mongoose').model('Foo', fooSchema)
Faraday will create this model in the database and you can access it asynchronously as:
require('faraday').modoose(({Foo}) ->
foo = Foo.find({bar: 'baz'})
# Do things with the Foo modoose.
)
Views
In your views
folder, put files the form filename.html.underscorecoffee
. These will be compiled as underscore.js (HTML) templates. You can invoke them as require('faraday').views.filename(scope)
. The scope
object will become the scope in the underscore.js template. That way you can pass variables and data for the template to render.
Controllers
In your controller
folder, put coffeescript files. If you create a file ./controllers/foo.coffee
with the following:
myRequestHandler = (req, res) ->
#handle the request
module.exports = myRequestHandler
Then navigate to localhost:3000/foo
, myRequestHandler will be invoked with two arguments: the node.js request object and response object. I commonly require views in a controller and invoke some corresponding view.
Faraday will map directories in the controllers folder to path components in the url. For example, if you save the aforementioned coffescript as ./controllers/several/nested/directories/foo.coffee
myRequestHandler will be invoked when you navigate to localhost:3000/several/nested/directories/foo
. You can also create path components by nesting the routing object:
myRequestHandler = (req, res) ->
#handle the request
module.exports =
several:
nested:
directories:
foo: myRequestHandler
You can inspect how this works by reading node_modules/faraday/route.coffee
.