了解TodoMVC例 [英] Understanding TodoMVC Example

查看:134
本文介绍了了解TodoMVC例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始学习的Node.js和Backbone.js的,并使用TodoMVC例子作为我的指导我。还有我无法左右环绕我的头两部分组成。见下文。

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.

下面是app.js.

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");

继承人是./models:

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);

安迪最后,这里是./routes:

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));

所以我的问题是,如何路由模块中mongoose.model('藤')的'藤'的模式在此范围内可用?我看到模型模块出口mongoose.model('藤',TodoSchema);所以我不得不相信这是路由模块如何访问它,但我不知道为什么。我在想什么?我有一种感觉它只是没有一个完整的范围,在这种情况下理解。另外,我不知道有路由功能匿名的推理。

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.

非常感谢!

推荐答案

这是比较令人困惑的事情要处理的节点和猫鼬开始出来的时候一个。

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

要求('猫鼬')首次,它创建一个的猫鼬实例 - 同一个实例返回您需要它的每一个随后的时间

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

这使得它可以很容易的工作,但有点神奇这就是开头难明白。

This makes it really easy to work with, but is a bit of 'magic' that's hard to understand at the beginning.

这意味着,当你调用 mongoose.connect(127.0.0.1,todomvc,27017); 在app.js,它会创建一个持续的连接与应用程序。

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

这也意味着, mongoose.model('藤',TodoSchema); 使得调用要求任何其他范围可用的藤堂模型( '猫鼬'),通过 mongoose.model('藤')。这可能在另一文件的顶部被var'd你需要如在上面的例子中,或瞬间需要它在回调的中间

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

这是你如何让藤堂模型到routes.js,和一个很好的理由,以确保猫鼬告诉你的车型的您在应用程序做的第一件事情之一。

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

要回答你的理解有关问题的范围;每个文件要求有效地都有自己的范围,并且没有获得任何东西,除了像处理全局对象。你必须要求你想要的一切的工作,并且只能通过调用功能或创建是通过暴露类传递变量出口对象。

To answer your questions regarding understanding scopes; each file you require effectively has its own scope and doesn't have access to anything except global objects like process. You have to require everything you want to work with, and can only pass variables in by calling functions or creating classes that are exposed via the exports object.

因此​​,对于实际的例子上面有出口从models.js模型,因为它不是随后引用的应用程序没有任何好处。在需要models.js的。它在routes.js这些线,使现有的藤堂模型:

So for the actual example above there is no benefit in exporting the model from models.js as it's not subsequently referenced in app.'s where models.js is required. It's these 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

这是藤在这条线是如何存在:

That's how Todo exists on this line:

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

有也没有好处(据我所知)在一个匿名函数包装的路线,因为这基本上是由规定要求

There's also no benefit (as far as I know) in wrapping the routes in an anonymous function as this is essentially provided by require.

这篇关于了解TodoMVC例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆