简体   繁体   English

如何在ejs中包含promise值?

[英]How to Include promise value in ejs?

I am making a small node application with help of express and ejs . 我在expressejs帮助下制作了一个小型节点应用程序。 In one of my controllers I return an object that has a promised property that I would like to include in my view. 在我的一个控制器中,我返回一个对象,该对象具有一个我要包含在我的视图中的promise属性。 I was trying to figure out what is the best approach to do this, since it seems ejs can't handle promised values. 我试图找出执行此操作的最佳方法,因为ejs似乎无法处理承诺的值。 I thought about including the promised value in the model instead, but it seems I overly complicated the code needed to be done. 我本来打算在模型中包含承诺值,但是似乎我过于复杂了需要完成的代码。

How can I present a bluebird promised value easily in ejs ? 如何在ejs轻松呈现bluebird承诺的价值?

Controller 控制者

compaintsRouter.get('/', function(req, res) 
{
    var model = data.complaint.findAndCount({
        include: [data.user, {
            model: data.comment,
            include: [data.user]
        }],
        limit: 15,
        offset: 15 * (req.query.page || 0) 
    }).then(function(result){
        res.render('complaints/list', {model: result.rows});
    });
});

ejs View template ejs视图模板

<% model.forEach(function(complaint) { %>

    <div class="complaint">
        ... more html here...
        <footer>
            <span class="vote brand-success">
                <% complaint.votesUp().then(function(votes){%> 
                    <%= votes %> 
                <%}) %>
                <span class="glyphicon glyphicon-thumbs-up clickable"></span>
            </span>
        </footer>
        ... more html here...
    </div>

<% }) %>

Promised method: 答应的方法:

votesUp: function() {
    return this.getFollower().then(function(followers){
        var aux = followers
            .selectMany(function(f){return f.UserComplaint})
            .map(function(v){ 
                return ((v.liked === true) ? 1: 0);
            });
        aux.push(0);
        aux.reduce(function(acc, v){
            return acc+v;
        });
    })
}

Seems like the answer might be mix business logic into the view so much. 似乎答案可能是将业务逻辑混入视图中。 Can you get the votes earlier so that you don't have to mess with it in the template? 您可以早些时候获得选票,这样就不必在模板中搞砸了吗? Seems pretty messy. 看起来很乱。 :) :)

(ps, +1 to offset the downvote) (ps,+ 1以抵消下降投票)

It seems I was missing a functionality of my orm, which was sequelize. 似乎我遗漏了orm的功能,这是续传的。 They allow me to retrieve the association as long as I include the model on my query. 只要我将模型包括在查询中,它们就允许我检索关联。 The modifications needed to be done were the following: 需要做的修改如下:

Controller 控制者

compaintsRouter.get('/', function(req, res) 
{
    var model = data.complaint.findAndCount({
        include: [data.user, {
            model: data.comment,
            include: [data.user]
        }, {
            model: data.user,
            as: 'follower'
        }],
        limit: 15,
        offset: 15 * (req.query.page || 0) 
    }).then(function(result){
        res.render('complaints/list', {model: result.rows});
    });
});

Method that uses the data: 使用数据的方法:

votesUp: function() {
        var aux = this.follower
            .selectMany(function(f){return f.UserComplaint})
            .map(function(v){ 
                return ((v.liked === true) ? 1: 0);
            });
        aux.push(0);
        return aux.reduce(function(acc, v){
            return acc+v;
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM