We’re developing a SPA using Durandal 2.0. The application is a dashboard-type of application. That is, there could be multiple dashboards, and, on each dashboard, there could be multiple widgets (our application’s widgets, not Durandal widgets – although that’s part of the question later).
From a viewmodel, we make an ajax call to the server to get the data about dashboards and widgets, something like:
data: [
{"dashboardId": 1, "dashboardName": "First Dashboard",
"widgets": [{"widgetId": 1, "dashboardId": 1, "widgetType": "Alarms", "widgetName": "abc"},{"widgetId": 2, "dashboardId": 1, "widgetType": "Links", "widgetName": "xyz"},{ next widgets }, ... ]},
{"dashboardId": 2, "dashboardName": "Second Dashboard",
"widgets": [{"widgetId": 9, "dashboardId": 2, "widgetType": "Reports", "widgetName": "123"},{ next widgets }, ... ]},
{ next dashboards and widgets }]
As you can see, each widget could be of a different type (e.g., Alarms, Reports, etc.), which means different styling and data inside the widget.
Currently, we have the view/viewmodel building the multiple dashboards (i.e., basically containers for widgets), but we’re not sure what is the best way to handle the individual widgets.
We initially thought of using external templates (i.e., a separate template for each widget type), but then we thought we read somewhere that Durandal doesn’t support external templating. We then thought maybe just use Durandal’s composition engine or Durandal widgets.
So, given our scenario, what is the recommended approach for dynamically building widgets on our view?
External templates (does Durandal even support external templates)?
Using the ko.compose binding? Something like:
<!-- ko if: widgetIsAlarms() -->
<!-- ko compose: { view: 'widgets/alarms/alarmswidget' } --><!-- /ko -->
<!-- /ko -->
Or, should we try to create a “Durandal widget” for each widget type and then somehow dynamically place the Durandal widget on the view depending on the type?
Or, is there some other recommended way?
Thanks!