如何让 D3 读取 Handlebars 上下文对象
•浏览 1
How can i get D3 to read a Handlebars context object
我正在使用 Node Express.js。我的一条路线呈现了 Handlebars 模板及其上下文。相关部分:
my_data = JSON.parse(body);
response.render( 'activity', my_data);<script type="text/javascript">
Handlebars.registerHelper('json', function(context) {
return JSON.stringify(context);
});
var datas = {{json my_data}}
d3.select("body")
.datum(datas)
.enter()
#do stuff with datavar express = require('express'),
exphbs = require('express3-handlebars'),
app = express(),
hbs;
hbs = exphbs.create({
// Specify helpers which are only registered on this instance.
helpers: {
// Your custom helper here
json: function(context) { return JSON.stringify(context); }
}
});
// Register it
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
我的问题:在那个 activity.hbs 模板中,我如何使用 D3 从 my_data JSON 对象创建一个绘图。在 activity.hbs 的相关部分我已经尝试过
my_data = JSON.parse(body);
response.render( 'activity', my_data);<script type="text/javascript">
Handlebars.registerHelper('json', function(context) {
return JSON.stringify(context);
});
var datas = {{json my_data}}
d3.select("body")
.datum(datas)
.enter()
#do stuff with datavar express = require('express'),
exphbs = require('express3-handlebars'),
app = express(),
hbs;
hbs = exphbs.create({
// Specify helpers which are only registered on this instance.
helpers: {
// Your custom helper here
json: function(context) { return JSON.stringify(context); }
}
});
// Register it
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
我在阅读此问题后添加了 Helper。当我尝试渲染视图时,它给出了一个错误:
Missing helper: 'json' at Object.
使用 Helper 是解决此问题的正确方法吗?如何让 D3 读取我的上下文对象?
我认为问题出在你的助手定义中,基本上你在错误的地方定义了助手。
要定义一个助手,根据 express3-handlebars 的文档,您必须在您的 express 应用代码中注册它:
my_data = JSON.parse(body);
response.render( 'activity', my_data);<script type="text/javascript">
Handlebars.registerHelper('json', function(context) {
return JSON.stringify(context);
});
var datas = {{json my_data}}
d3.select("body")
.datum(datas)
.enter()
#do stuff with datavar express = require('express'),
exphbs = require('express3-handlebars'),
app = express(),
hbs;
hbs = exphbs.create({
// Specify helpers which are only registered on this instance.
helpers: {
// Your custom helper here
json: function(context) { return JSON.stringify(context); }
}
});
// Register it
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');