Skip to content

Commit

Permalink
Replaced all tabs with spaces and fixed some alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan51 committed Oct 7, 2010
1 parent 9724942 commit d03cf0a
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 332 deletions.
124 changes: 62 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ How do I use Ni?

It's as simple as telling Ni where to look for your files, and then asking it to boot:

var Ni = require('../lib/ni');
Ni.setRoot(__dirname);
Ni.boot(function() {
// Ready to start the server!
}
var Ni = require('../lib/ni');
Ni.setRoot(__dirname);
Ni.boot(function() {
// Ready to start the server!
}

The rest of your code now has access to all your models, views, and controllers in `Ni.models`, `Ni.views` and `Ni.controllers`.

Expand All @@ -34,14 +34,14 @@ Ni provides a router you can use with Connect to have requests sent to the appro

Use it with Connect:

var app = Connect.createServer(
Ni.router
// You can add other Connect middle-ware here
);
var app = Connect.createServer(
Ni.router
// You can add other Connect middle-ware here
);

It parses the URL and sends the request to the correct controller function as follows:

http://yourapp.com/[controller]/[function]/[argument 1]/[argument 2]/[etc]
http://yourapp.com/[controller]/[function]/[argument 1]/[argument 2]/[etc]

If no controller is specified (`http://yourapp.com/`), it loads the `home` controller's `index` function.

Expand All @@ -52,15 +52,15 @@ Can I see an example?

If you have your project organized like this:

/app.js
/controllers
/calculator.js
/home.js
/views
/calculator.html
/home.html
/models
/calculator.js
/app.js
/controllers
/calculator.js
/home.js
/views
/calculator.html
/home.html
/models
/calculator.js

You can access your stuff with:

Expand All @@ -75,49 +75,49 @@ How would my controllers, models, libraries, helpers look?

Each of those is just a Node module. For example, the calculator controller mentioned above (in the `/example` folder) looks like this:

var Ni = require('../../lib/ni'),
Mu = require('mu'),
Quip = require('quip');

var CalculatorController = function() {
/*
* This function is called when the URL does not indicate a function to
* be called, so it would look like /calculator.
*/

this.index = function(req, res) {
res.ok('Welcome to the calculator!');
}

/*
* This function is called when the URL indicates "add" as the function
* to be called, so it would look like /calculator/add.
*
* For example, loading the URL /calculator/add/4/5 would call the below
* function with a = 4 and b = 5.
*/
this.add = function(req, res, a, b) {
if (a && b) {
a = parseInt(a);
b = parseInt(b);

var template = Ni.views.calculator.template;
var data = {result: a + b};

var compiled = Mu.compileText(template, null);
compiled(data).addListener('data', function (c) {
res.ok(c);
});
}
else {
res.error("a and b must both be provided.");
}
}
};

module.exports = new CalculatorController();
var Ni = require('../../lib/ni'),
Mu = require('mu'),
Quip = require('quip');

var CalculatorController = function() {
/*
* This function is called when the URL does not indicate a function to
* be called, so it would look like /calculator.
*/

this.index = function(req, res) {
res.ok('Welcome to the calculator!');
}

/*
* This function is called when the URL indicates "add" as the function
* to be called, so it would look like /calculator/add.
*
* For example, loading the URL /calculator/add/4/5 would call the below
* function with a = 4 and b = 5.
*/
this.add = function(req, res, a, b) {
if (a && b) {
a = parseInt(a);
b = parseInt(b);

var template = Ni.views.calculator.template;
var data = {result: a + b};

var compiled = Mu.compileText(template, null);
compiled(data).addListener('data', function (c) {
res.ok(c);
});
}
else {
res.error("a and b must both be provided.");
}
}
};

module.exports = new CalculatorController();

How to organize your code
-------------------------
Expand Down
72 changes: 36 additions & 36 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
/*
* This is an example of how to use Ni to organize your code into a nice,
* neat MVC project.
/*
* This is an example of how to use Ni to organize your code into a nice,
* neat MVC project.
*
* You can place your controllers, models, views, libraries and helpers into
* respective folders /controllers, /models, /views, /libraries, /helpers, and
* they will be loaded when you call Ni.boot into the Ni object.
* You can place your controllers, models, views, libraries and helpers into
* respective folders /controllers, /models, /views, /libraries, /helpers, and
* they will be loaded when you call Ni.boot into the Ni object.
*
* Take a look at the example controllers and views for how to structure that
* code to make it integrate with Ni.
* Take a look at the example controllers and views for how to structure that
* code to make it integrate with Ni.
*/

/*
* Module dependencies
* Module dependencies
*/

var Connect = require('connect'),
Quip = require('quip'),
Ni = require('../lib/ni');
Quip = require('quip'),
Ni = require('../lib/ni');

/*
* Load Ni and start the server.
* Load Ni and start the server.
*/

Ni.setRoot(__dirname); // Tells Ni where to look for the folders
Ni.config.location = 'world'; // Sets a custom configuration variable to use
// later, anywhere
Ni.setRoot(__dirname); // Tells Ni where to look for the folders
Ni.config.location = 'world'; // Sets a custom configuration variable to use
// later, anywhere

Ni.boot(function() { // Boots Ni and loads everything
var app = Connect.createServer( // Create server when Ni is finished
// booting
Quip(), // Helps in sending HTTP responses
Ni.router, // The Ni router automatically
// directs requests based on URL
// segments to the appropriate
// controller functions
function (req, res, next) { // Called if no controller /
// function for the URL given is
// found
res.notFound('Page not found.');
}
);
app.listen(3000);
Ni.boot(function() { // Boots Ni and loads everything
var app = Connect.createServer( // Create server when Ni is finished
// booting
Quip(), // Helps in sending HTTP responses
Ni.router, // The Ni router automatically
// directs requests based on URL
// segments to the appropriate
// controller functions
function (req, res, next) { // Called if no controller /
// function for the URL given is
// found
res.notFound('Page not found.');
}
);
app.listen(3000);

console.log('Application server started on port 3000');
console.log('Application server started on port 3000');
});
94 changes: 47 additions & 47 deletions example/controllers/calculator.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
/*
* CalculatorController - a controller to be used with Ni that is called by
* the router when a user visits a URL that starts with /calculator.
* CalculatorController - a controller to be used with Ni that is called by
* the router when a user visits a URL that starts with /calculator.
*
* Controllers are represented as Javascript objects, and exported as a Node
* module after the object definition.
*
* Functions in controllers are called with the request and result as arguments
* (just like Connect functions are called) and then with the rest of the
* segments of the URL as the rest of the arguments.
* Controllers are represented as Javascript objects, and exported as a Node
* module after the object definition.
*
* Functions in controllers are called with the request and result as arguments
* (just like Connect functions are called) and then with the rest of the
* segments of the URL as the rest of the arguments.
*
* For example, loading the URL /calculator/add/4/5 would call the add function
* below with a = 4 and b = 5.
* For example, loading the URL /calculator/add/4/5 would call the add function
* below with a = 4 and b = 5.
*
* If no function is specified in the URL (such as the URL /calculator), then
* then index function is called if it exists.
* If no function is specified in the URL (such as the URL /calculator), then
* then index function is called if it exists.
*
* Note: The req argument gives you access to the request, and you can use the
* res argument to send back a response to the browser or requester.
* Note: The req argument gives you access to the request, and you can use the
* res argument to send back a response to the browser or requester.
*/

/*
* Module dependencies
* Module dependencies
*/

var Ni = require('../../lib/ni'),
Mu = require('mu'),
Quip = require('quip');
Mu = require('mu'),
Quip = require('quip');

/*
* The calculator controller
* The calculator controller
*/

var CalculatorController = function() {
/*
* This function is called when the URL does not indicate a function to
* be called, so it would look like /calculator.
*/
/*
* This function is called when the URL does not indicate a function to
* be called, so it would look like /calculator.
*/

this.index = function(req, res) {
res.ok('Welcome to the calculator!');
}
this.index = function(req, res) {
res.ok('Welcome to the calculator!');
}

/*
* This function is called when the URL indicates "add" as the function
* to be called, so it would look like /calculator/add.
*/
this.add = function(req, res, a, b) {
if (a && b) {
a = parseInt(a);
b = parseInt(b);
/*
* This function is called when the URL indicates "add" as the function
* to be called, so it would look like /calculator/add.
*/
this.add = function(req, res, a, b) {
if (a && b) {
a = parseInt(a);
b = parseInt(b);

var template = Ni.views.calculator.template;
var data = {result: a + b};
var template = Ni.views.calculator.template;
var data = {result: a + b};

var compiled = Mu.compileText(template, null);
compiled(data).addListener('data', function (c) {
res.ok(c);
});
}
else {
res.error("a and b must both be provided.");
}
}
var compiled = Mu.compileText(template, null);
compiled(data).addListener('data', function (c) {
res.ok(c);
});
}
else {
res.error("a and b must both be provided.");
}
}
};

/*
* Exports the calculator controller
* Exports the calculator controller
*/

module.exports = new CalculatorController();
Loading

0 comments on commit d03cf0a

Please sign in to comment.