Skip to content

Commit

Permalink
Setup readme and package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ajorkowski committed Mar 9, 2012
1 parent cee19c2 commit 58bd8a6
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 43 deletions.
42 changes: 0 additions & 42 deletions Cakefile
@@ -1,7 +1,6 @@
fs = require 'fs'
{print} = require 'util'
{spawn, exec} = require 'child_process'
#watchit = require 'watchit'

build = (watch, callback) ->
if typeof watch is 'function'
Expand All @@ -15,49 +14,8 @@ build = (watch, callback) ->
coffee.stderr.on 'data', (data) -> print data.toString()
coffee.on 'exit', (status) -> callback?() if status is 0

###
task 'docs', 'Generate annotated source code with Docco', ->
fs.readdir 'src', (err, contents) ->
files = ("src/#{file}" for file in contents when /\.coffee$/.test file)
docco = spawn 'docco', files
docco.stdout.on 'data', (data) -> print data.toString()
docco.stderr.on 'data', (data) -> print data.toString()
docco.on 'exit', (status) -> callback?() if status is 0
###

task 'build', 'Compile CoffeeScript source files', ->
build()

task 'watch', 'Recompile CoffeeScript source files when modified', ->
build true

###
task 'test', 'Run the test suite (and re-run if anything changes)', ->
suite = null
build ->
do runTests = ->
suite?.kill()
suiteNames = [
'DevelopmentIntegration'
'ProductionIntegration'
'AbsoluteIntegration'
'RemoteIntegration'
'BenchmarkDynamic'
'BenchmarkStatic'
]
suiteIndex = 0
do runNextTestSuite = ->
return unless suiteName = suiteNames[suiteIndex]
suite = spawn "coffee", ["-e", "{reporters} = require 'nodeunit'; reporters.default.run ['#{suiteName}.coffee']"], cwd: 'test'
suite.stdout.on 'data', (data) -> print data.toString()
suite.stderr.on 'data', (data) -> print data.toString()
suite.on 'exit', -> suiteIndex++; runNextTestSuite()
invoke 'docs' # lest I forget
watchTargets = (targets..., callback) ->
for target in targets
watchit target, include: true, (event) ->
callback() unless event is 'success'
watchTargets 'src', -> build runTests
watchTargets 'test', 'Cakefile', runTests
###
Empty file removed README
Empty file.
119 changes: 119 additions & 0 deletions README.md
@@ -0,0 +1,119 @@
# Controllers

A simple mvc framework and route extender for Express.

### Usage

After setting all your middleware in Express, call the controllers method to initialise.

```
express = require 'express'
controllers = require 'controllers'
app = express.createServer()
app.use(express.static(__dirname + '/public'));
# Make sure all your app.use statements have been called
controllers app, options
```

Your folder system should now look like the following:

```
site
|-> controllers
| |-> home.js (or coffee, if you have overwritten the require calls)
| |-> blog.js
|-> views
| -> home
| |-> index.jade (these are your views, use whatever renderer you want)
| |-> welcome.jade
| -> blog
| | -> index.jade
| -> shared
| -> layout.jade
```

Controllers are called depending on your routing, and the render call is overwritten to access the folder with the same name as the controller, falling back to the shared folder if needed.

### Routing

When routing a controller and action must be defined, controllers extends the routing in Express to allow for default values

```
# app.get 'route', defaults, middleware...
app.get '/blogPage', { controller: 'blog', action: 'index' }, middleware
app.get '/:controller?/:action?/:id?', { controller: 'home', action: 'index' }, middleware
```

The above routing will route the following paths:

```
'/' -> Routes to the controller 'home' and runs the method 'index'
'/blogPage' -> Routes to the controller 'blog' and runs the method 'index'
'/home/welcome/1' -> Routes to the controller 'home' and runs the method 'welcome', with the 'id' param set to 1
```
### What does a controller look like?

The controller actions follow the normal convention of Express, taking the request, response and next arguments:

```
module.exports.index = (req, res, next) ->
res.render()
module.exports.welcome = (req, res, next) ->
id = req.param.id ?? 0
res.partial { id: id }
```

The render does not take an argument as the view for this action is automatically searched for in at 'views/home/index' and if that fails falls back to 'views/shared/index'.

### Helpers

There are a number of useful calls available in the controllers and views.

Controllers:
```
req.controller - stores current controller
req.action - stores current action
req.executeController 'controller', 'action', cb - Executes another controller and overwrites the next function with the cb
```

Views:
```
controller - stores current controller
action - stores current action
getUrl 'controller', 'action', defaultParams, queryParams - returns a url corresponding to the controller/action specified
getUrl 'action', defaultParams, queryParams - same as above but using the current controller
```

### Options

The default options are:

```
# If the controller/action is not defined do we throw an exception?
strict: true
# Overwrite the render/partial calls to use the 'controller/action' breakdown of the views
overwriteRender: true
# Log when the controllers are loaded and called
log: false
# Set the root folder for the controllers
root: app.set('controllers') || process.cwd() + '/controllers'
# Set the share folder in the views, all render/partial calls will fall back to this folder
sharedFolder: 'shared'
```

### License

©2012 Felix Jorkowski and available under the [MIT license](http://www.opensource.org/licenses/mit-license.php):

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions package.json
@@ -0,0 +1,16 @@
{
"author": "Felix Jorkowski (http://jorkowski.com)",
"name": "controllers",
"description": "A simple mvc framework and route extender for Express",
"version": "0.0.1",
"homepage": "https://github.com/ajorkowski/controllers",
"repository": {
"type": "git",
"url": "git://github.com/ajorkowski/controllers.git"
},
"main": "lib/controllers.js",
"dependencies": {
},
"devDependencies": {
}
}
2 changes: 1 addition & 1 deletion src/controllers.coffee
Expand Up @@ -23,7 +23,7 @@ class Controllers
controller = path.basename reduced
self._controllers[controller] = require reduced
if self.options.log
console.log "Controller '" + controller + "' has been loaded"
console.log "Controller '#{controller}' has been loaded"

# We are off to hijack the req.app.routes._route
# which is the point of contact of all our get/post/pull/etc methods.
Expand Down

0 comments on commit 58bd8a6

Please sign in to comment.