0

从 MEAN.io 开始,他们提供了一个示例“文章”模型,基本上类似于带有标题和正文的博客文章。

该示例附带一个index.html文件,当您导航到该文件时会显示文章列表。在这个文件中,它调用了find在公共控制器中定义的方法

$scope.find = function() {      
  Articles.query(function(articles) {
     $scope.articles = articles;
  });      
};

我看到一个定义以下方法的服务器控制器

/**
 * List of Articles
 */
exports.all = function(req, res) {
  Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
    if (err) {
      return res.json(500, {
        error: 'Cannot list the articles'
      });
    }
    res.json(articles);
  });
};

find当我在服务器控制器中为方法添加约束时,我可以有效地where为查询定义过滤器,这反映在视图中。

框架隐式处理的这两个控制器之间是否存在某种联系?我找不到有关所有这些如何相关的任何信息。

4

1 回答 1

0

恕我直言,没有。如果有过滤连接,代码必须是这样的

/**
 * List of Articles
 *  use GET /api/v1/articles?published=true to filter
 */
exports.all = function(req, res) {
  Article
        .find(req.query) //this is filtering!
        .sort('-created')
        .populate('user', 'name username')
        .exec(function(err, articles) {
    if (err) {
      return res.json(500, {
        error: 'Cannot list the articles'
      });
    }
    res.json(articles);
  });
};
于 2014-09-09T20:19:49.487 回答