【问题标题】:Send handlebars data to client-side javascript将车把数据发送到客户端 javascript
【发布时间】:2016-01-20 17:18:57
【问题描述】:

我正在构建一个 node.js 应用程序并且我正在使用把手。我想检查是否有办法将车把模板中定义的数据发送到客户端 javascript 以用于计算。例如,这是我的控制器:

module.exports = function (req, res) {

    var projectId = req.params.id;
    var context = {
        cost: 500,
        revenue: 1000, 
    }

    res.render('../views/reports', context)
};

在客户端的 javascript 文件中,我想做类似的事情:

$(document).ready(function(context) {
    var profit = context.revenue - context.cost;
});

我不确定如何将上下文传递到客户端 javascript。

有人可以帮忙吗?

谢谢!

【问题讨论】:

    标签: javascript node.js handlebars.js


    【解决方案1】:

    客户

    $.post("/endpoint_on_server",
    {
        id: "xyz"
    },
    function(context){ //the result of the res.render
        //update the view
        $('body').html(context);
    });
    

    服务器

    app.post('/endpoint_on_server', function(req, res){
      var projectId = req.params.id; //xyz
      var context = {
        cost: 500,
        revenue: 1000, 
      }
    
      res.render('../views/reports', context)
    }
    

    【讨论】:

    • id属性有什么作用?
    • 它包含值xyz,在请求正文中发送。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2013-04-11
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多