Permalink
Browse files

0.0.4

  • Loading branch information...
1 parent 2fdf338 commit af876485825cae65332f9c0841f633b0281b1660 @morozovsk committed Jun 25, 2012
Showing with 78 additions and 51 deletions.
  1. +1 −2 Readme.md
  2. +7 −7 controller.js
  3. +49 −21 index.js
  4. +3 −3 package.json
  5. +10 −9 samples/sample-with-modules/index.js
  6. +8 −9 samples/sample-without-modules/index.js
View
@@ -1,5 +1,4 @@
-This is light weight mvc layer for express on node.js
-This mvc framework like ZendFramework (php)
+This is light weight mvc wrapper for express on node.js inspired by Zend Framework on php.
###installation
View
@@ -1,4 +1,4 @@
-module.exports = function (mvc, app, request, response) {
+module.exports = function (mvc, app, request, response, settings) {
this._app = app;
this._request = request;
this._response = response;
@@ -51,14 +51,14 @@ module.exports = function (mvc, app, request, response) {
this._url = function (params) {
var url = '/';
- if (params.module !== undefined && params.module !== app.set('defaultModule') || params.controller !== undefined && params.controller !== app.set('defaultController') || params.action !== undefined && params.action !== app.set('defaultController')) {
- url += (params.module == undefined ? app.set('defaultModule') : params.module);
+ if (params.module !== undefined && params.module !== settings.defaultModule || params.controller !== undefined && params.controller !== settings.defaultController || params.action !== undefined && params.action !== settings.defaultController) {
+ url += (params.module == undefined ? settings.defaultModule : params.module);
- if (params.controller !== undefined && params.controller !== app.set('defaultController') || params.action !== undefined && params.action !== app.set('defaultController')) {
- url += '/' + (params.controller == undefined ? app.set('defaultController') : params.controller);
+ if (params.controller !== undefined && params.controller !== settings.defaultController || params.action !== undefined && params.action !== settings.defaultController) {
+ url += '/' + (params.controller == undefined ? settings.defaultController : params.controller);
- if (params.action !== undefined && params.action !== app.set('defaultController')) {
- url += '/' + (params.action ? app.set('defaultAction') : params.action);
+ if (params.action !== undefined && params.action !== settings.defaultController) {
+ url += '/' + (params.action ? settings.defaultAction : params.action);
}
}
}
View
@@ -1,16 +1,33 @@
module.exports.app = {}
module.exports.controllers = {}
module.exports.models = {}
+module.exports.settings = {}
+
+module.exports.init = function(app, settings) {
+ var defaultSettings = {
+ applicationDirectory: 'app',
+ moduleDirectory: 'modules',
+ controllerDirectory: 'controllers',
+ modelDirectory: 'models',
+ defaultModule: 'index',
+ defaultController: 'index',
+ defaultAction: 'index',
+ viewDirectory: 'views',
+ controllerCache: true,
+ modelCache: true
+ }
-module.exports.init = function(app) {
- if (app.set('applicationDirectory') === undefined) app.set('applicationDirectory', '/app/');
- if (app.set('moduleDirectory') === undefined) app.set('moduleDirectory', '/modules/');
- if (app.set('controllerDirectory') === undefined) app.set('controllerDirectory', '/controllers/');
- if (app.set('modelDirectory') === undefined) app.set('modelDirectory', '/models/');
- if (app.set('viewDirectory') === undefined) app.set('viewDirectory', '/views/');
- if (app.set('defaultModule') === undefined) app.set('defaultModule', 'index');
- if (app.set('defaultController') === undefined) app.set('defaultController', 'index');
- if (app.set('defaultAction') === undefined) app.set('defaultAction', 'index');
+ if (settings === undefined) {
+ settings = defaultSettings;
+ } else {
+ for (var i in defaultSettings) {
+ if (settings[i] === undefined) {
+ settings[i] = defaultSettings[i];
+ }
+ }
+ }
+
+ module.exports.settings = settings;
module.exports.app = app;
var util = require('util');
@@ -19,7 +36,7 @@ module.exports.init = function(app) {
var loadModule = function (modulePath, moduleName) {
var loadComponents = function (type) {
- var componentsPath = path.join(modulePath, app.set(type + 'Directory'));
+ var componentsPath = path.join(modulePath, settings[type + 'Directory']);
//console.log(componentsPath);
module.exports[type + 's'][moduleName] = {};
@@ -43,6 +60,17 @@ module.exports.init = function(app) {
var component = require(componentPath);
module.exports[type +'s'][moduleName][componentName] = component;
+
+ if ((type == 'controller' && !settings['controllerCache']) || (type == 'model' && !app.settings['modelCache'])) {
+ fs.watchFile(componentPath, function(event, filename) {
+ try {
+ delete(require.cache[componentPath]);
+ module.exports[type +'s'][moduleName][componentName] = require(componentPath);
+ } catch (e){
+ //console.log(e.stack.toString());
+ }
+ });
+ }
}
}
}
@@ -51,11 +79,11 @@ module.exports.init = function(app) {
loadComponents('model');
}
- var appPath = path.join(process.cwd() ,app.set('applicationDirectory'));
+ var appPath = path.join(process.cwd(), settings.applicationDirectory);
- loadModule(appPath, app.set('defaultModule'));
+ loadModule(appPath, settings.defaultModule);
- var modulesPath = path.join(process.cwd(), app.set('applicationDirectory'), app.set('moduleDirectory'));
+ var modulesPath = path.join(process.cwd(), settings.applicationDirectory, settings.moduleDirectory);
//console.log(util.inspect(module.exports));
@@ -77,13 +105,13 @@ module.exports.init = function(app) {
//load models and controllers;
app.all('/:module?/:controller?/:action?', function(request, response, next) {
- request.params.module = request.param('module', app.set('defaultModule'));
- request.params.controller = request.param('controller', app.set('defaultController'));
- request.params.action = request.param('action', app.set('defaultAction'));
+ request.params.module = request.param('module', settings.defaultModule);
+ request.params.controller = request.param('controller', settings.defaultController);
+ request.params.action = request.param('action', settings.defaultAction);
if (module.exports.controllers[request.params.module] == undefined) {
request.params.controller = request.params.module;
- request.params.module = app.set('defaultModule');
+ request.params.module = settings.defaultModule;
}
//res.end(util.inspect(request.params));return;
@@ -102,17 +130,17 @@ module.exports.init = function(app) {
//var path = require('path');
- if (request.params.module == app.set('defaultModule')) {
- app.set('views', process.cwd() + path.join(app.set('applicationDirectory'), app.set('viewDirectory')));
+ if (request.params.module == settings.defaultModule) {
+ app.set('views', path.join(process.cwd(), settings.applicationDirectory, settings.viewDirectory));
} else {
- app.set('views', process.cwd() + path.join(app.set('applicationDirectory'), app.set('moduleDirectory'), request.params.module, app.set('viewDirectory')));
+ app.set('views', path.join(process.cwd(), settings.applicationDirectory, settings.moduleDirectory, request.params.module, settings.viewDirectory));
}
//console.log(app.set('views'));
try {
var helper = require('mvc/controller');
- helper = new helper(module.exports, module.exports.app, request, response);
+ helper = new helper(module.exports, module.exports.app, request, response, settings);
for (var i in helper) {
controller[i] = helper[i];
}
View
@@ -1,11 +1,11 @@
{
"name": "mvc",
- "description": "mvc for expess",
- "version": "0.0.3",
+ "description": "mvc wrapper for express like Zend framework on php",
+ "version": "0.0.4",
"homepage": "https://github.com/morozovsk/mvc",
"repository": "https://github.com/morozovsk/mvc",
"author": "morozovsk <morozovsk@gmail.com> (https://github.com/morozovsk)",
- "keywords": ["mvc", "express", "expressjs", "connect", "framework", "zf", "zend", "php"],
+ "keywords": ["mvc", "express", "expressjs", "connect", "framework", "zf", "zend", "zendframework", "php"],
"engines": { "node": "*" },
"dependencies": {
"express": "*"
@@ -12,14 +12,6 @@ app.configure(function(){
app.set('view options', {complexNames: true});
app.set('jsDirectory', '/js/');
app.set('cssDirectory', '/css/');
- app.set('applicationDirectory', '/app/');
- app.set('moduleDirectory', '/modules/');
- app.set('controllerDirectory', '/controllers/');
- app.set('modelDirectory', '/models/');
- app.set('defaultModule', 'index');
- app.set('defaultController', 'index');
- app.set('defaultAction', 'index');
- app.set('viewDirectory', '/views/');
app.use(express.bodyParser());
app.use(express.cookieParser('secret'));
app.use(express.session({secret: 'secret'}));
@@ -29,7 +21,16 @@ app.configure(function(){
app.use(app.router);
});
-require('mvc').init(app);
+require('mvc').init(app, {
+ applicationDirectory: 'app',
+ moduleDirectory: 'modules',
+ controllerDirectory: 'controllers',
+ modelDirectory: 'models',
+ defaultModule: 'index',
+ defaultController: 'index',
+ defaultAction: 'index',
+ viewDirectory: 'views'
+});
app.all('*', function(req, res) {
res.send('errror 404', 404);
@@ -12,14 +12,6 @@ app.configure(function(){
app.set('view options', {complexNames: true});
app.set('jsDirectory', '/js/');
app.set('cssDirectory', '/css/');
- app.set('applicationDirectory', '/app/');
- app.set('moduleDirectory', '/modules/');
- app.set('controllerDirectory', '/controllers/');
- app.set('modelDirectory', '/models/');
- app.set('viewDirectory', '/views/');
- app.set('defaultModule', 'index');
- app.set('defaultController', 'index');
- app.set('defaultAction', 'index');
app.use(express.bodyParser());
app.use(express.cookieParser('secret'));
app.use(express.session({secret: 'secret'}));
@@ -29,7 +21,14 @@ app.configure(function(){
app.use(app.router);
});
-require('mvc').init(app);
+require('mvc').init(app, {
+ applicationDirectory: 'app',
+ controllerDirectory: 'controllers',
+ modelDirectory: 'models',
+ defaultController: 'index',
+ defaultAction: 'index',
+ viewDirectory: 'views'
+});
app.all('*', function(req, res) {
res.send('errror 404', 404);

0 comments on commit af87648

Please sign in to comment.