Starting to learn node.js and backbone.js and am using the TodoMVC example as my guide. There are a couple parts I am having trouble wrapping my head around. See below.
Here is app.js.
var express = require('express')
, http = require('http')
, mongoose = require('mongoose')
, models = require('./models')
, routes = require('./routes')
, app = express();
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function () {
app.use(express.errorHandler());
});
routes.init(app);
mongoose.connect("127.0.0.1", "todomvc", 27017);
http.createServer(app).listen(3000);
console.log("Express server listening on port 3000");
Heres is ./models:
var mongoose = require('mongoose'),
TodoSchema = new mongoose.Schema({
title: { 'type': String, 'default': 'empty todo...' },
order: { 'type': Number },
done: { 'type': Boolean, 'default': false }
});
module.exports = mongoose.model('Todo', TodoSchema);
Andy finally, here is ./routes:
(function (exports) {
"use strict";
var mongoose = require('mongoose')
, crudUtils = require('../utils/crudUtils')
, Todo = mongoose.model('Todo');
function index(req, res) {
res.render('index', { 'title': 'Backbone.js, Node.js, MongoDB Todos' });
}
exports.init = function (app) {
app.get('/', index);
crudUtils.initRoutesForModel({ 'app': app, 'model': Todo });
};
}(exports));
So my question is, how is the 'Todo' model in mongoose.model('Todo') in the routes module available in this scope? I see that the models module is exporting mongoose.model('Todo', TodoSchema); so I have to believe that is how the routes module has access to it, but I don't know why. What am I missing? I have a feeling its just not a complete understanding of scope in this situation. Also, I am not sure of the reasoning of having the routes function anonymous.
Many thanks!