【发布时间】:2017-11-04 09:42:59
【问题描述】:
我使用 express 和车把构建了一个简单的应用程序
在我的模型中,我有这样的函数原型
// 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}}
【问题讨论】:
-
为什么不让
full_name成为吸气剂?Object.defineProperty(Student.prototype, "full_name", {get(){ return this.first_name + " " + this.last_name }})或class Student { get full_name(){ return ... }} -
这是我在训练营中得到的要求。我在这里做任务
-
然后您可以注册一个帮助您调用该方法的助手。
-
我现在不明白helper,你能给我一些线索吗?
标签: javascript node.js express handlebars.js