0
对yeoman的默认meanjs应用程序有疑问。mean.js使用require without var
的express.js文件里面有像这样的声明:
// Globbing model files
config.getGlobbedFiles('./app/models/**/*.js').forEach(function(modelPath) {
require(path.resolve(modelPath));
});
现在我明白,它得到所有的路径“./app/models/”里面的.js文件,但我我试图理解是独自站立
require(path.resolve(modelPath));
如何使用require函数而不将其设置为“var”?
这些包含文件的一个的一个例子是这样的:
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
content: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Article', ArticleSchema);
此文件不公开什么。
那么,为什么要求用“var”调用并且没有暴露函数的内容呢?
这将如何允许内容稍后使用?
好的。我对你的解释有了更好的理解。 – 2015-02-10 05:44:17
另一个问题,但。代码'mongoose.model('Article',ArticleSchema)'注册对象'Article'在哪里?现在看我可以用“Article.query();”来调用它。 它是否被加载为某种全局对象? – 2015-02-10 05:46:32
从现在开始,您不能只使用'文章',它必须包含在使用它的每个文件的顶部。例如,在'articles.server.controller.js'中,前几行中有'var Article = mongoose.model('Article');' – tkounenis 2015-02-10 06:25:42