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
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: It's as simple as telling Ni where to look for your files, and then asking it to boot:


var Ni = require('../lib/ni'); var Ni = require('../lib/ni');
Ni.setRoot(__dirname); Ni.setRoot(__dirname);
Ni.boot(function() { Ni.boot(function() {
// Ready to start the server! // 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`. 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: Use it with Connect:


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


It parses the URL and sends the request to the correct controller function as follows: 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. 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: If you have your project organized like this:


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


You can access your stuff with: 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: 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'), var Ni = require('../../lib/ni'),
Mu = require('mu'), Mu = require('mu'),
Quip = require('quip'); Quip = require('quip');


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


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


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


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


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


module.exports = new CalculatorController(); module.exports = new CalculatorController();


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


/* /*
* Module dependencies * Module dependencies
*/ */


var Connect = require('connect'), var Connect = require('connect'),
Quip = require('quip'), Quip = require('quip'),
Ni = require('../lib/ni'); 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.setRoot(__dirname); // Tells Ni where to look for the folders
Ni.config.location = 'world'; // Sets a custom configuration variable to use Ni.config.location = 'world'; // Sets a custom configuration variable to use
// later, anywhere // later, anywhere


Ni.boot(function() { // Boots Ni and loads everything Ni.boot(function() { // Boots Ni and loads everything
var app = Connect.createServer( // Create server when Ni is finished var app = Connect.createServer( // Create server when Ni is finished
// booting // booting
Quip(), // Helps in sending HTTP responses Quip(), // Helps in sending HTTP responses
Ni.router, // The Ni router automatically Ni.router, // The Ni router automatically
// directs requests based on URL // directs requests based on URL
// segments to the appropriate // segments to the appropriate
// controller functions // controller functions
function (req, res, next) { // Called if no controller / function (req, res, next) { // Called if no controller /
// function for the URL given is // function for the URL given is
// found // found
res.notFound('Page not found.'); res.notFound('Page not found.');
} }
); );
app.listen(3000); 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
@@ -1,73 +1,73 @@
/* /*
* CalculatorController - a controller to be used with Ni that is called by * CalculatorController - a controller to be used with Ni that is called by
* the router when a user visits a URL that starts with /calculator. * the router when a user visits a URL that starts with /calculator.
* *
* Controllers are represented as Javascript objects, and exported as a Node * Controllers are represented as Javascript objects, and exported as a Node
* module after the object definition. * module after the object definition.
* *
* Functions in controllers are called with the request and result as arguments * 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 * (just like Connect functions are called) and then with the rest of the
* segments of the URL as the rest of the arguments. * segments of the URL as the rest of the arguments.
* *
* For example, loading the URL /calculator/add/4/5 would call the add function * For example, loading the URL /calculator/add/4/5 would call the add function
* below with a = 4 and b = 5. * below with a = 4 and b = 5.
* *
* If no function is specified in the URL (such as the URL /calculator), then * If no function is specified in the URL (such as the URL /calculator), then
* then index function is called if it exists. * then index function is called if it exists.
* *
* Note: The req argument gives you access to the request, and you can use the * 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. * res argument to send back a response to the browser or requester.
*/ */


/* /*
* Module dependencies * Module dependencies
*/ */


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


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


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


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


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


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


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


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


module.exports = new CalculatorController(); module.exports = new CalculatorController();

0 comments on commit d03cf0a

Please sign in to comment.