Permalink
Browse files

Changed name of automatic view renderer to renderView

  • Loading branch information...
1 parent 64b8397 commit 75acd22e588507925485e7714306264d2f69536a @chetan51 committed Nov 11, 2010
Showing with 68 additions and 24 deletions.
  1. +6 −6 README.md
  2. +1 −1 example/controllers/calculator.js
  3. +61 −17 lib/ni.js
View
@@ -21,9 +21,9 @@ It's as simple as telling Ni where to look for your files, and then asking it to
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`.
+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.
@@ -64,9 +64,9 @@ 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!
@@ -103,7 +103,7 @@ Each of those is just a Node module. For example, the calculator controller ment
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);
@@ -52,7 +52,7 @@ var CalculatorController = function() {
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);
View
@@ -19,6 +19,7 @@ var fs = require('fs'),
*/
var Ni = function() {
+
var Ni = this;
/*
@@ -135,20 +136,61 @@ var Ni = function() {
}
}
- this.view = function (renderer) {
- if (typeof(renderer) === 'function') {
- Ni.automatic_views = true;
- return function (req, res, next) {
- if (res.Ni.view && typeof(res.Ni.action) !== 'undefined') {
- renderer(req, res, next, res.Ni.controller + '/' + res.Ni.action);
- } else {
- next();
- }
- }
- } else {
- throw new Error('Ni.view expects a function as the first argument');
- }
+ this.renderView = function (renderer) {
+ if (typeof(renderer) === 'function') {
+ Ni.automatic_views = true;
+ return function (req, res, next) {
+ if (req.Ni.view && typeof(req.Ni.action) !== 'undefined') {
+ renderer(req, res, next, req.Ni.controller + '/' + req.Ni.action);
+ } else {
+ next();
+ }
+ }
+ } else {
+ throw new Error('Ni.renderView expects a function as the first argument');
+ }
+ }
+
+ /*
+ * Gets a specific controller by name.
+ */
+
+ this.controller = function(name) {
+ return Ni.controllers[name];
+ }
+
+ /*
+ * Gets a specific model by name.
+ */
+
+ this.model = function(name) {
+ return Ni.models[name];
}
+
+ /*
+ * Gets a specific view by name.
+ */
+
+ this.view = function(name) {
+ return Ni.views[name];
+ }
+
+ /*
+ * Gets a specific library by name.
+ */
+
+ this.library = function(name) {
+ return Ni.libraries[name];
+ }
+
+ /*
+ * Gets a specific helper by name.
+ */
+
+ this.helper = function(name) {
+ return Ni.helpers[name];
+ }
+
};
/* Helper functions
@@ -239,10 +281,12 @@ function loadFilesFromDir(dir, fnOnFile, callbackDone) {
if (files.length > 0) {
var group = this.group();
files.forEach(function (file){
- var name = file.split('.')[0];
-
- fnOnFile(dir, file, name);
- group()();
+ if (! /~$/.test(file)) {
+ var name = file.split('.')[0];
+
+ fnOnFile(dir, file, name);
+ group()();
+ }
});
}
else {

0 comments on commit 75acd22

Please sign in to comment.