Understanding TodoMVC Example

Starting to learn node.js and backbone.js, and I use the TodoMVC example as my guide. There are a couple of parts that I'm having trouble wrapping my head around. See below.

Here is the 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. / 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 area? I see the model module exporting mongoose.model ('Todo', TodoSchema); so I have to believe that the route module has access to it, but I donโ€™t know why. What am I missing? I feel that this is simply not a complete understanding of the scope in this situation. Also, I'm not sure if using anonymous routes will be anonymous.

Many thanks!

+6
source share
2 answers

This is one of the most confusing things to deal with when starting up in Node and Mongoose.

When you require('mongoose') for the first time, it creates a singleton instance of Mongoose - the same instance is returned each time you need it.

This simplifies the job, but it's a bit of โ€œmagicโ€ that is hard to understand at the beginning.

This means that when you call mongoose.connect("127.0.0.1", "todomvc", 27017); in app.js, it creates a connection that is saved with the application.

It also means mongoose.model('Todo', TodoSchema); makes the Todo model available in any other area that calls require('mongoose') through mongoose.model('Todo') . It can be var'd at the top of another file that you require , as in the example above, or at the moment when you need it in the middle of the callback.

This is how you get the Todo model into your .js routes, and a very good reason to tell Mongoose about your models is one of the first things you do in your application.

To answer your questions regarding areas of understanding; each require file has its own scope and does not have access to anything other than global objects such as a process. You must require everything you want to work with, and you can only pass variables by calling functions or creating classes that are displayed through the exports object.

Thus, for the actual example above, there is no use in exporting a model from models.js, since it is not mentioned later in the application. where models.js is required. These are the lines in routes.js that make the Todo model available:

 var mongoose = require('mongoose') , Todo = mongoose.model('Todo'); // returns the Todo model that was registered by models.js 

How Todo exists on this line:

 crudUtils.initRoutesForModel({ 'app': app, 'model': Todo }); 

There is also no use (as far as I know) for wrapping routes in an anonymous function, since this is essentially provided by require .

+8
source

You will need to check Express.

Express is a minimal and flexible node.js web application framework that provides a robust set of features for creating single and multi-page and hybrid web applications.

A lot of what you copy and paste uses Express Skaffold - so thereโ€™s a lot to offer you: you can check it out here: http://expressjs.com/

Hope you head in the right direction.

0
source