Permalink
Browse files

Merge commit 'upstream/master'

Conflicts:

	lib/ni.js
  • Loading branch information...
2 parents 38b07fa + d2a88e7 commit 89fcf1041438ddb6d13c087e56c2c252d2a3ffda @maritz maritz committed Nov 23, 2010
Showing with 197 additions and 52 deletions.
  1. +21 −12 README.md
  2. +4 −2 example/app.js
  3. +7 −3 example/controllers/calculator.js
  4. +4 −2 example/controllers/home.js
  5. +161 −33 lib/ni.js
View
@@ -9,7 +9,7 @@ What's awesome about Ni?
---------------------------
* It's packaged as a regular Node module, so you just `require` it and you're ready to go
-* You can use other Node modules and Connect middle-ware as you usually would; Ni does not get in the way
+* You can use other Node modules and Connect middle-ware as you usually would; Ni doesn't get in the way
* It's easy to use and loads your models, views, controllers, libraries and helpers automatically so you can just start using them everywhere
How do I use Ni?
@@ -18,12 +18,14 @@ 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.config('root', "myapp/src");
+
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`.
+The rest of your code now has access to all your models, views, and controllers with `Ni.model('MODELNAME')`, `Ni.view('VIEWNAME')` and `Ni.controller('CONTROLLERNAME')`.
Note that your controllers, models, libraries and helpers should be packaged as Node modules, and your views can be plain text, template (Markdown, Mustache, etc), or HTML files.
@@ -41,9 +43,9 @@ Use it with Connect:
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://myapp.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://myapp.com/`), it loads the `home` controller's `index` function.
If no function is specified (http://yourapp.com/[controller]), it loads the `[controller]`'s `index` function.
@@ -64,12 +66,14 @@ If you have your project organized like this:
You can access your stuff with:
-* `Ni.controllers.calculator`
-* `Ni.views.calculator`
-* `Ni.models.calculator`
+* `Ni.controller('calculator')`
+* `Ni.view('calculator')`
+* `Ni.model('calculator')`
A really well-commented example is in the source code in the `/example` folder, check it out!
+(Note that you'll need the following to run the example in the `/example` folder: [Connect](https://github.com/senchalabs/Connect), [Quip](https://github.com/caolan/quip) and [Mu](https://github.com/raycmorgan/Mu). You get get them easily using [npm](https://github.com/isaacs/npm).)
+
How would my controllers, models, libraries, helpers look?
-------------------------------------------------------
@@ -86,8 +90,10 @@ Each of those is just a Node module. For example, the calculator controller ment
* be called, so it would look like /calculator.
*/
- this.index = function(req, res) {
+ this.index = function(req, res, next) {
+
res.ok('Welcome to the calculator!');
+
}
/*
@@ -98,12 +104,13 @@ Each of those is just a Node module. For example, the calculator controller ment
* function with a = 4 and b = 5.
*/
- this.add = function(req, res, a, b) {
+ this.add = function(req, res, next, a, b) {
+
if (a && b) {
a = parseInt(a);
b = parseInt(b);
- var template = Ni.views.calculator.template;
+ var template = Ni.view('calculator').template;
var data = {result: a + b};
var compiled = Mu.compileText(template, null);
@@ -114,7 +121,9 @@ Each of those is just a Node module. For example, the calculator controller ment
else {
res.error("a and b must both be provided.");
}
+
}
+
};
module.exports = new CalculatorController();
View
@@ -22,8 +22,8 @@ var Connect = require('connect'),
* 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
+Ni.config('root', __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
@@ -42,10 +42,12 @@ Ni.boot(function() { // Boots Ni and loads everything
// function for the URL given is
// found
res.notFound('Page not found.');
+
}
);
app.listen(3000);
console.log('Application server started on port 3000');
+
});
@@ -38,21 +38,24 @@ var CalculatorController = function() {
* be called, so it would look like /calculator.
*/
- this.index = function(req, res) {
+ this.index = function(req, res, next) {
+
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) {
+ this.add = function(req, res, next, a, b) {
+
if (a && b) {
a = parseInt(a);
b = parseInt(b);
- var template = Ni.views.calculator.template;
+ var template = Ni.view('calculator').template;
var data = {result: a + b};
var compiled = Mu.compileText(template, null);
@@ -63,6 +66,7 @@ var CalculatorController = function() {
else {
res.error("a and b must both be provided.");
}
+
}
};
@@ -16,9 +16,11 @@ var Ni = require('../../lib/ni');
*/
var HomeController = function() {
- this.index = function(req, res) {
- res.ok('Hello ' + Ni.config.location + '!');
+
+ this.index = function(req, res, next) {
+ res.ok('Hello ' + Ni.config('location') + '!');
}
+
};
/*
Oops, something went wrong.

0 comments on commit 89fcf10

Please sign in to comment.