controller.example.js | |
---|---|
Class hive.ControllerAn example of a simple hive.Controller class. TypesThere are two types of Controllers in hive. A traditional class / instanced based Controller that allows for inheritence and a simplified shorthand functional version. Either mechanism you choose, in order for hive to load your class automatically, you must include it at you-project/controllers/controllername.js Dependencies | var hive = require('../lib/hive'); |
In order to expose your controller as a class you'll need to export it; this will let it be used as a module, eg. | exports = module.exports = hive.Controller.extend({ |
Each Controller needs a unique name. The convention is to use the singular version of a the model which you are attempting to control, eg. | _name: 'User', |
Controllers have a root, which must be preceded by a | at: '/user', |
RoutesRoutes allow you to DRY up your controllers by removing routing code from your actions. They are made up of a key to match and a description of where to go. You can see more examples here. | routes: {
'/did/:action': {action: 'activity'}
}, |
ActionsActions correspond to things your model can do. In our example, our user can register, login, logout, and tweet.
Each action can point to a | actions: {
post: {
register: hive.models.User
},
delete: {
logout: hive.models.Session
},
login: function (req, res) {
return view({
model: new MySession(req.body),
redirect: '/dashboard',
success: function() {
res.cookie('auth', model.get('auth'), {httpOnly: true});
}
});
},
tweet: function () {
},
before: {
register: function (req, res) {
}
},
after: {
login: function (result) {
}
}
}
}); |
TestsHive tests are written in vowjs and hive itself, we encourage you to follow the example of our docs to write your own tests. | var vows = require('vows'),
fs = require('fs'),
assert = require('assert'),
controller = require('../lib/controller'),
_ = require('underscore');
Http = hive.Http; |
This will test our above example controller. | vows.describe('controller').addBatch({
topic: function() {
var _self = this,
request = new Http({url: 'http://localhost:3000/widget/about'});
request.fetch();
request.ready(function () {
_self.callback(request.errors, request);
});
},
'The example controller': {
'should respond to an http get request at http://localhost:3000/widget/about': function(err, request) {
assert.equal(request.get('data'), sampleResponse);
}
}
});
|