本文介绍了蒙戈指定查询参数从客户机控制器(MEAN.JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用MongoDB的,棱角分明,前preSS的应用程序,和节点(MEAN堆栈)。

I am building an application using MongoDB, Angular, Express, and Node (MEAN stack).

我用MEAN.JS发电机脚手架我的申请。

I used the MEAN.JS generator to scaffold my application.

我将使用模块的文章作为参考。

I will use the articles module as a reference.

假如我有7000条记录在我的文章集合,每条记录都有一个与之相关联的日期。这是一种低效每次都记录7000加载到内存我加载页面在表中查看记录,我看到的,因为它可怕的性能损失。出于这个原因,我只想加载记录与(1个月前)范围内的日期(1从现在起一年),并在表中显示出来。我目前与以下做到这一点:

Suppose I have 7000 records in my articles collection, and each record has a date associated with it. It is inefficient to load all 7000 records into memory every time I load the page to view the records in a table and I am seeing terrible performance losses because of it. For this reason, I would only like to load records with a date in the range of (1 Month Ago) to (1 Year From Now) and display them in the table. I can currently do this with the following:

在我articles.client.controller.js:

In my articles.client.controller.js:

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

...在我的articles.server.controller.js:

...and in my articles.server.controller.js:

var now = new Date();
var aYearFromNow = new Date(now.getTime() + 86400000*365); //add a year
var aMonthAgo = new Date(now.getTime() - 86400000*30); //subtract roughly a month

exports.list = function(req, res) { Article.find().where('date').lt(aYearFromNow).gt(aMonthAgo).sort('-created').populate('user', 'displayName').exec(function(err, articles) {
        if (err) {
            return res.send(400, {
                message: getErrorMessage(err)
            });
        } else {
            res.jsonp(articles);
        }
    });
};

的问题是,这不是一个做事的动态的方式。换句话说,我希望用户能够指定前进,他们希望回来多远多远看。

The problem is that this is not a dynamic way of doing things. In other words, I want the user to be able to specify how far back and how far forward they want to see.

如何绑定变量(例如'aYearFromNow'和'aMonthAgo')在我的客户认为这将改变我的服务器控制器的查询参数?

推荐答案

这可能不是最彻底的方法,但您可以创建一个新的服务(或编辑当前有几个参数来工作):

It's probably not the cleanest way, but you can create a new service (or edit the current one to work with several parameters):

.factory('ArticlesService2', ['$resource',
    function($resource) {
        return $resource('articles/:param1/:param2', {
            param1: '',
            param2: ''
        }, {
            update: {
                method: 'PUT'
            }
        });
    }
]);

然后把它在你的控制器:

Then call it in your controller :

$scope.findWithParams = function() {
    $scope.article = ArticlesService2.query({
        param1: $scope.aYearFromNow,
        param2: $scope.aMonthAgo
    });
};

在后端,你需要prepare路线:

On the back-end, you'll need to prepare a route :

app.route('/articles/:param1/:param2')
    .get(articles.listWithParams)

添加一个函数来后端控制器:

Add a function to your back-end controller :

exports.listWithParams = function(req, res) {
    Article.find()
    .where('date')
    .lt(req.params.param1)
    .gt(req.params.param2)
    .sort('-created').populate('user', 'displayName')
    .exec(function(err, articles) {
        if (err) {
            return res.send(400, {
                message: getErrorMessage(err)
            });
        } else {
            res.jsonp(articles);
        }
    });
};

应该工作,没有虽然进行了测试。

Should work, haven't tested it though.

这篇关于蒙戈指定查询参数从客户机控制器(MEAN.JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:13
查看更多