Chapter 7 - Move the slideshow data to the service¶
In this chapter, you will move the slideshow data to the AppService file to allow more flexibility in the making of the slideshow. If you ever need to update the slideshow, you can do it in one place without having to modify the Handlebars template.
import{Injectable}from'@nestjs/common';constslideshow=[{src:"https://source.unsplash.com/random?1",type:"image",alt:"Random photo on Unsplash",interval:5000,},{src:"https://source.unsplash.com/random?2",type:"image",alt:"Random photo on Unsplash",interval:5000,},{src:"https://source.unsplash.com/random?3",type:"image",alt:"Random photo on Unsplash",interval:5000,},{src:"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4",type:"video",alt:"Sintel: first open movie",interval:10000,},]@Injectable()exportclassAppService{getSlideshow(){returnslideshow;}}
Handlebars offers some built-in helpers that you can use in the template.
Some of the built-in helpers are if and each as seen earlier for example.
However, the if helper only evaluates boolean (true/false). As such, you have to create a new custom helper eq that can compare two objects. If they are equal, return true, otherwise, false.
Update the main file to add this custom helper to Handlebars.
The eq helper can then be used in the template as seen earlier.
This peculiar version of the function (v1,v2)=>v1===v2 is the same as function(v1,v2){returnv1===v2}. It's a shorter syntax that is helpful in this use case.