我不知道这种方法是否错误,如果是这样,请告诉我,因为我是NodeJs的新手。但我试图在Handlebars中实现,以访问一个方法。我的结构方式是这样的:我有一个系列组件,其中包含以下内容:
在Series.js中,我有一个函数可以使用简单的select all SQL来获取所有系列。然后我创建一个SeriesDAL的新实例,并将所有结果包装在SeriesDal类中。在SeriesDal中,我将每个结果包装到SeriesService中。
然后我有一个SeriesDal类,其中包含一系列SeriesServices。
现在我有两个问题
第一个是我的结果集如下:
SeriesDal {
series:
[ SeriesService { serie: [Object] },
SeriesService { serie: [Object] } ] }
在我看来,这意味着我必须在循环中循环才能到达该系列。
我可以以某种方式达到这样的东西:
SeriesDal {
[ SeriesService { [Object] },
SeriesService { [Object] } ] }
So that I only have to loop through it once?
第二件事是,现在在我的视图中,在我的循环中我在SeriesService条目上,假设我有一个自定义的getLinkRef我将如何访问此getLinkRef方法/属性
我目前的代码示例:https://pastebin.com/C2fupLye
在此先感谢希望有人可以帮助我。
答案 0 :(得分:1)
看起来你已经通过这个DAL和服务层创造了很多复杂性,这引起了一些混乱。
如果您希望SeriesService具有SeriesService.serie具有的所有属性,那么只需将所有这些属性分配给Service的构造函数即可。这将给出
function SeriesService(serie) {
// Ditch the this.serie object which is causing your layer of nesting
this.link = serie.link;
this.name = serie.name;
// Etc...
}
SeriesService.prototype.getLinkRef = function() {
// Now this can access everything via "this" and it should be valid
return "<a href='" . this.link . "'>" . this.name . "</a>";
};
module.exports = SeriesService;