Permalink
Browse files

updated to 0.2

  • Loading branch information...
1 parent d008991 commit c177e9b168f461b4602252c63ab44841fc5671b8 @MaksJS committed Feb 22, 2013
Showing with 67 additions and 84 deletions.
  1. +30 −25 README.md
  2. +2 −1 example/app.coffee
  3. +20 −20 example/controllers/Players.coffee
  4. +1 −1 example/package.json
  5. +1 −1 package.json
  6. +13 −36 src/simple_mvc.coffee
View
@@ -10,7 +10,7 @@ It provides a simple way to create models and controllers with an object-oriente
## How it works
-Create your express application as usual and use the "simple_mvc" function
+Create your express application as usual and use the "simple_mvc" function and pass the path to the controllers, models and routes directories.
```coffeescript
express = require 'express'
@@ -19,9 +19,16 @@ require 'namespace'
app = express()
app.listen 3000
-simple_mvc app
+
+app.configure ->
+ app.use express.methodOverride()
+ app.use app.router
+
+simple_mvc app, __dirname + '/controllers', __dirname + '/models', __dirname + '/config/routes'
+
```
+You must configure your application before using simple_mvc.
You'll also need a namespace pattern for coffeescript, you can install it with npm install
Create a directory called "config" with a file called "routes" inside.
@@ -53,43 +60,41 @@ namespace controllers:
{Player} = models
# GET /players
- index: ->
- Player.all (players) =>
- @res.send players
+ @index: (req, res) ->
+ Player.all (players) ->
+ res.send players
# GET /player/:id
- show: ->
- Player.read @req.params.id, (player) =>
- @res.send player
+ @show: (req, res) ->
+ Player.read req.params.id, (player) ->
+ res.send player
# GET /players/new
- new: ->
- @res.render 'newForm'
+ @new: (req, res) ->
+ res.render 'newForm'
# GET /player/:id/edit
- edit: ->
- Player.read @req.params.id, (player) =>
- @res.render 'editForm'
+ @edit: (req, res) ->
+ Player.read req.params.id, (player) ->
+ res.render 'editForm'
player: player
# POST /players
- create: ->
- Player.create @req.param('player'), =>
- @res.send 'New player successfully created !'
+ @create: (req, res) ->
+ Player.create req.param('player'), ->
+ res.send 'New player successfully created !'
# PUT /player/:id
- update: ->
- Player.update @req.params.id, @req.param('player'), =>
- @res.send "Player #{@req.param('id')} successfully updated !"
+ @update: (req, res) ->
+ Player.update req.params.id, req.param('player'), ->
+ res.send "Player #{req.param('id')} successfully updated !"
# DELETE /player/:id
- delete: ->
- Player.delete @req.params.id, ->
- @res.send "Player #{@req.param('id')} successfully deleted !"
+ @delete: (req, res) ->
+ Player.delete req.params.id, ->
+ res.send "Player #{req.param('id')} successfully deleted !"
```
-Properties @req and @res are automatically send to the controller, you don't need to pass them as params as you would do before with Express
-
And the for the model (I use mongoose here, but you can adapt it)
```coffeescript
@@ -136,4 +141,4 @@ namespace models:
do cb
```
-And that's all ! Looks difficult ? Have a look at the source code in /src (only 50 lines !) or run the example.
+And that's all ! Looks difficult ? Have a look at the source code in /src (only about 20 lines !) or run the example.
View
@@ -5,11 +5,12 @@ require 'namespace'
app = express()
app.listen 3000
-simple_mvc app
app.configure ->
app.use express.methodOverride()
app.use app.router
+simple_mvc app, __dirname + '/controllers', __dirname + '/models', __dirname + '/config/routes'
+
mongoose.connect 'mongodb://127.0.0.1:27017/test', (err) ->
if err then throw err
@@ -6,36 +6,36 @@ namespace controllers:
{Player} = models
# GET /players
- index: ->
- Player.all (players) =>
- @res.send players
+ @index: (req, res) ->
+ Player.all (players) ->
+ res.send players
# GET /player/:id
- show: ->
- Player.read @req.params.id, (player) =>
- @res.send player
+ @show: (req, res) ->
+ Player.read req.params.id, (player) ->
+ res.send player
# GET /players/new
- new: ->
- @res.render 'newForm'
+ @new: (req, res) ->
+ res.render 'newForm'
# GET /player/:id/edit
- edit: ->
- Player.read @req.params.id, (player) =>
- @res.render 'editForm'
+ @edit: (req, res) ->
+ Player.read req.params.id, (player) ->
+ res.render 'editForm'
player: player
# POST /players
- create: ->
- Player.create @req.param('player'), =>
- @res.send 'New player successfully created !'
+ @create: (req, res) ->
+ Player.create req.param('player'), ->
+ res.send 'New player successfully created !'
# PUT /player/:id
- update: ->
- Player.update @req.params.id, @req.param('player'), =>
- @res.send "Player #{@req.param('id')} successfully updated !"
+ @update: (req, res) ->
+ Player.update req.params.id, req.param('player'), ->
+ res.send "Player #{req.param('id')} successfully updated !"
# DELETE /player/:id
- delete: ->
- Player.delete @req.params.id, ->
- @res.send "Player #{@req.param('id')} successfully deleted !"
+ @delete: (req, res) ->
+ Player.delete req.params.id, ->
+ res.send "Player #{req.param('id')} successfully deleted !"
View
@@ -1,7 +1,7 @@
{
"name": "express-simple-mvc",
"description": "example",
- "version": "0.1.0",
+ "version": "0.2.0",
"scripts": {
"start": "coffee app"
},
View
@@ -1,6 +1,6 @@
{
"name": "express-simple-mvc",
- "version": "0.1.1",
+ "version": "0.2.0",
"description": "simple and lightweight framework to improve Express JS MVC's architecture",
"main": "src/simple_mvc.coffee",
"dependencies": {
View
@@ -1,44 +1,21 @@
-fs = require 'fs'
-
-getModels = (cb) ->
- fs.readdir './models', (err, files) ->
- if err then throw err
- require __dirname + '/../../../models/' + file for file in files
- do cb
-
-getControllers = (cb) ->
- fs.readdir './controllers', (err, files) ->
- if err then throw err
- require __dirname + '/../../../controllers/' + file for file in files
- do cb
-
-getRoutes = (cb) ->
- fs.readFile './config/routes', 'utf8', (err, routes) ->
- if err then throw err
- cb routes
-
-parseRoutes = (app, routes) ->
+exports.simple_mvc = (app, dirControllers, dirModels, dirRoutes) ->
+ fs = require 'fs'
+ # Getting models
+ require dirModels + '/' + model for model in fs.readdirSync dirModels
+ # Getting controllers
+ require dirControllers + '/' + controller for controller in fs.readdirSync dirControllers
+ # Getting routes
+ routes = fs.readFileSync dirRoutes, 'utf8'
+ # Parsing routes
for route in routes.split('\n') when route.match /^[A-Z]/
route = route.split /\s+/
rest = route[0].toLowerCase()
url = route[1]
- controller = route[2].match(/(.+)\.(\w+)$/)[1]
- method = route[2].match(/(.+)\.(\w+)$/)[2]
+ method = route[2]
try
- controller = eval controller
- controller = new controller
+ method = eval method
catch e
- throw new Error "Controller '#{controller}' doesn't exists"
- throw new Error "Controller '#{controller}' doesn't have '#{method}' method" unless controller[method]?
+ throw new Error "Method '#{route[2]}' doesn't exists"
throw new Error "REST method '#{rest}' not supported" if rest not in ['get', 'post', 'put', 'delete', 'head', 'all']
throw new Error "Invalid URL '#{url}' in routes" unless /^\/\w*/.test url
- app[rest] url, do (controller, method) -> (req, res) ->
- controller.req = req
- controller.res = res
- do controller[method]
-
-exports.simple_mvc = (app) ->
- getModels ->
- getControllers ->
- getRoutes (routes) ->
- parseRoutes app, routes
+ app[rest] url, do (controller, method) -> (req, res) -> method req, res

0 comments on commit c177e9b

Please sign in to comment.