[英]calling function prototype in handlebars
我使用快递和车把构建了一个简单的应用程序
在我的模型中,我有这样的函数原型
// myModel.js
Student.prototype.getFullname = function () {
return `${this.first_name} ${this.last_name}`;
}
在我的路由器中,我可以像这样调用函数原型
// myRouter.js
const rows = await Model.Student.findAll();
console.log(rows[0].getFullname()); // I can invoke function prototype here
res.render('mypage', rows); // with express, render it to hbs
我的问题:如何在车把中调用函数原型?
{{#each this}}
<tr>
<td>{{ id }}</td>
<td>{{ first_name }}</td>
<td>{{ last_name }}</td>
<td>{{ rows[0].getFullname() }}</td> // I wanna call it here
</tr>
{{/each}}
在handlebars helpers docs中 ,从字面上看,您的示例带有fullName
helper。
注册助手:
Handlebars.registerHelper('getFullName', function(student) {
return `${student.first_name} ${student.last_name}`;
});
使用助手:
{{#each this}}
<tr>
<td>{{ id }}</td>
<td>{{ first_name }}</td>
<td>{{ last_name }}</td>
<td>{{ getFullName this }}</td>
</tr>
{{/each}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.