Skip to content

Commit

Permalink
add notfound error behavior for view
Browse files Browse the repository at this point in the history
  • Loading branch information
jrf0110 committed May 19, 2014
1 parent d6e58e1 commit e5fdf8f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -210,4 +210,20 @@ app.get( '/users/:id'
);
```
### ```dm.find
__Special Options:__
To override `error` and `not found` behavior:
```javascript
app.get( '/users/:id'
, dm.param( 'id' )
, dm.returning( 'id', 'name' )
, dm.view( 'single_user_view', db.users, {
layout: 'admin/layout'
, notFound: function( req, res ){ res.render('404'); }
, error: function( error, req, res ){ res.render('error', { error: error }); }
})
);
```
### TODO: FINISH DOCS
21 changes: 21 additions & 0 deletions lib/dirac-middleware.js
Expand Up @@ -114,6 +114,19 @@ m.view = function(name, collection, options){
options = options || {};
options.method = options.method || 'find';

var defaults = {
notFound: function( req, res ){
return res.send(404);
}
, error: function( error, req, res ){
return res.send(500);
}
};

for ( var key in defaults ){
if ( !( key in options ) ) options[ key ] = defaults[ key ];
}

return function(req, res){
collection = collection || req.collection;

Expand All @@ -133,6 +146,14 @@ m.view = function(name, collection, options){
}

args.push( function( error, results ){
if ( error ){
return options.error( error, req, res );
}

if ( !results ){
return options.notFound( req, res );
}

res.locals[
// If results is an array, we want to set the local
// view variable to the plural form of that object type
Expand Down

0 comments on commit e5fdf8f

Please sign in to comment.