[英]Express / Node.js - Render page with data
I have a list of Teams
displayed as a list of links on my page, at the moment, I have a textbox....when the link for that Team
is clicked, the Key
for that Team
is taken and sent to the /team/:key
route, it searches for the Team
by Key
, gets the data and sends the data back. 我有一个列表
Teams
显示为我的网页上链接的列表,在那一刻,我有一个文本框....的链接,当Team
被点击, Key
针对Team
采取并送到/team/:key
路线,它将搜索Team
由Key
,获取数据,并将数据发送回。 If the data was successfully sent back to the JS file then the Team
name is output to the textbox. 如果数据已成功发送回JS文件,则
Team
名称将输出到文本框。
I want to change this, when the link is clicked and the data has been retrieved using Team.findByKey
I want to render a new page ('teamDetails') and pass it the data for that Team
. 我想改变这一点,当点击链接并使用
Team.findByKey
检索数据时,我想渲染一个新页面('teamDetails')并将该数据传递给该Team
。 This way I can make it so that each link for a Team
links to the Team's
individual page with their full details on it. 这样我可以把它使每个环节的
Team
链接到Team's
个人页面与他们的它的全部细节。
Any idea how I would adapt this /team/:key
route to do this? 我知道如何调整这个
/team/:key
路线来做到这一点?
JS file
// Setup teamsLink to retrieve Team info for whichever Team was clicked
Team.initTeamsLink = function(){
$("a.teamLink").live("click", function(e){
e.preventDefault();
var teamId = ($(this)[0]).getAttribute('id');
$.get('/team/'+teamId, function(response){
if(response.retStatus === 'success'){
var teamData = response.teamData;
$('#teamId').val(teamData.key);
$('#teamName').val(teamData.name);
} else if(response.retStatus === 'failure'){
}
});
});
};
Route
app.get('/team/:key', function(req, res) {
util.log('Serving request for url [GET] ' + req.route.path);
Team.findByKey(req.params.key, function(err, teamData){
if(!err && teamData){
teamData = teamData;
res.json({
'retStatus' : 'success',
'teamData' : teamData
});
} else {
util.log('Error in fetching Team by key : ' + req.params.key);
res.json({
'retStatus' : 'failure',
'msg' : 'Error in fetching Team by key ' + req.params.key
});
}
});
});
Use handlebars templates - http://handlebarsjs.com/ 使用车把模板 - http://handlebarsjs.com/
npm install handlebars
npm install hbs
Coffe-script: 咖啡脚本:
hbs = require 'hbs'
app.set 'views', __dirname + '/app/views' #folder with your views
app.set 'view engine', 'hbs'
app.use express.bodyParser()
app.use express.methodOverride()
In the router: 在路由器中:
res.render 'index', {title: 'index'} #view name and context
Instead of res.json()
, you could use a templating engine which would render the individual team's page. 您可以使用模板引擎代替
res.json()
,该引擎将呈现单个团队的页面。 Say you want to use EJS templates: 假设您要使用EJS模板:
npm install ejs
Next, create views/team.ejs
, which would contain the EJS template for the team. 接下来,创建
views/team.ejs
,其中包含团队的EJS模板。 In your route, replace res.json()
with a call to the templating engine: 在您的路线中,将
res.json()
替换为对模板引擎的调用:
res.render('team.ejs', { 'teamData' : teamData });
EDIT : just read your comment about using Jade templates: the code is very similar, just replace team.ejs
with teamDetails.jade
. 编辑 :只是阅读你关于使用Jade模板的评论:代码非常相似,只需用
team.ejs
替换teamDetails.jade
。
Also, you probably can remove your client-side code which handles click events on the links, since you don't want to perform an AJAX request anymore. 此外,您可能可以删除处理链接上的单击事件的客户端代码,因为您不再需要执行AJAX请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.