partial.js logo
Latest version
v1.3.1 (RC2)

How do controllers work?


In controllers you can define routes and actions. Each action is executed by the route. In controller you can
define 3 types of route - HTTP/HTTPS, WebSocket and File.



Starting partial.js project


Controller definition


// Must be defined in each controller
exports.install = function(framework, name) {

	// this place is for definition routes (HTTP, CUSTOM STATIC FILES, WEBSOCKETS)
	// http://docs.partialjs.com/Framework/#framework.route
	framework.route('/', view_homepage);
	framework.route('/xhr/contact-form/', json_contactform, ['post', 'xhr']);

	// http://docs.partialjs.com/Framework/#framework.websocket
	framework.websocket('/', socket_chat, ['json']);

	// http://docs.partialjs.com/Framework/#framework.file
	framework.file('*.pdf download counter', file_download);

}

function view_homepage() {

	// this === controller
	var self = this;
	self.view('homepage');

}

function json_contactform() {

	// this === controller
	var self = this;

	// validate data
	// save data to DB
	// send e-mail
	...
	...
	...
	// return a response
	self.json({ r: true });

}

function socket_chat(controller, framework) {

	// Define websocket events
	// client === http://docs.partialjs.com/FrameworkWebSocketClient/
	controller.on('open', function(client) {

		// connect user
		console.log('Online:', controller.online);

		client.send({ message: 'Hello {0}'.format(client.id) });
		controller.send({ message: 'Connect new user: {0}\nOnline: {1}'.format(client.id, controller.online) }, [], [client.id]);

	});

	controller.on('close', function(client) {

		// disconnect user
		console.log('Online:', controller.online);
		controller.send({ message: 'Disconnect user: {0}\nOnline: {1}'.format(client.id, controller.online) });

	});

	controller.on('message', function(client, message) {
		// send to all without this client
		message.message = client.id + ': ' + message.message;
		controller.send(message);

	});

	controller.on('error', function(error, client) {
		console.log('WEBSOCKET ERROR', error);
	});

}

var counter = 0;

// Handling files is optimized for the performance
function file_download(req, res, isValidation) {

	if (isValidation)
		return req.url.toLowerCase().substring(req.url.length - 4) === '.pdf';

	// this === framework
	var self = this;
	var filename = path.basename(req.url);

	counter++;

	// http://docs.partialjs.com//Framework/#framework.responseFile
	self.responseFile(req, res, self.path.public(filename), filename);
};


How to share functions and models between controllers?


Controller supports sharing of models and functions between controllers. Sharing example.

Controller: users.js

In this controller we set functions and models for sharing.

exports.install = function(framework, name) {
	framework.route('/users/', view_users);
};

exports.models = {
	users: ['Peter', 'Lucia', 'Zuzana', 'Veronika']
};

exports.functions = {
	exists: function(name) {
		return exports.models.users.indexOf(name) > -1;
	}
};

function view_users() {
	this.json(exports.models.users);
}

Controller: default.js

Look to line 8. In this line we load controller functions from 'users.js' controller.

exports.install = function(framework, name) {
	framework.route('/', view_homepage);
};

function view_homepage() {
	var self = this;

	var users = self.functions('users');

	// console.log(self.models('users').users);

	var builder = [];

	builder.push('Exists: Peter = {0}'.format(users.exists('Peter')));
	builder.push('Exists: Jolaus = {0}'.format(users.exists('Jolaus')));
	builder.push('Exists: Lucia = {0}'.format(users.exists('Lucia')));
	builder.push('');
	builder.push('All users: {0}users/'.format(utils.path(self.req.uri.href)));

	self.plain(builder.join('\'));
}

Monitoring controllers


partial.js supports monitoring of controllers via controller usage. Usage example.

var counter = 0;

exports.install = function(framework, name) {
	framework.route('/', view_homepage);
	framework.route('/usage/', plain_usage);
};

exports.usage = function() {
	return 'Visits: ' + counter;
};

function view_homepage() {
	var self = this;
	counter++;
	self.view('homepage');
}

function plain_usage() {
	var self = this;
	self.plain(self.framework.usage(true));
}

Define system routes


All system routes are optional.

exports.install = function(framework) {
	framework.route('#400', error400);
	framework.route('#401', error401);
	framework.route('#403', error403);
	framework.route('#404', error404);
	framework.route('#408', error408);
	framework.route('#431', error431);
	framework.route('#500', error500);
};

// Bad Request
function error400() {
	var self = this;
	self.statusCode = 401;
	self.plain(utils.httpStatus(self.statusCode));
}

// Unauthorized
function error401() {
	var self = this;
	self.statusCode = 401;
	self.plain(utils.httpStatus(self.statusCode));
}

// Forbidden
function error403() {
	var self = this;
	self.statusCode = 403;
	self.plain(utils.httpStatus(self.statusCode));
}

// Not Found
function error404() {
	var self = this;
	self.statusCode = 404;
	self.plain(utils.httpStatus(self.statusCode));
}

// Request Timeout
function error408() {
	var self = this;
	self.statusCode = 408;
	self.plain(utils.httpStatus(self.statusCode));
}

// Request Header Fields Too Large
function error431() {
	var self = this;
	self.statusCode = 431;
	self.plain(utils.httpStatus(self.statusCode));
}

// Internal Server Error
function error500() {
	var self = this;
	self.statusCode = 500;
	self.plain(utils.httpStatus(self.statusCode));
}