Question

Using Durandal widgets, you have to create a folder for each widget, and then 2 files in each folder. For example a mywidget folder containing view.html, model.js files.

This naming style makes it difficult to find files in the solution. Is there any way to name these file names like mywidget.html, mywidget.js?

Was it helpful?

Solution

If you want to use your own convention, you can override the way that the Widget plugin loads its parts relatively easily. For example, if you wanted your convention to be that both the view and view model of the widget were named after the widget's kind, you could do this:

define(["plugins/widget"], function (widget) {
    // Look in 'widgets/[kind]' for '[kind].html' and '[kind].js'
    var convertKindToKind = function (kind) {
        return "widgets/" + kind + "/" + kind;
    };

    widget.convertKindToModulePath = convertKindToKind;
    widget.convertKindToViewPath = convertKindToKind;
});

Alternatively, you can use the mapKind function for a "one-off" non-conventional widget mapping:

// Parameters are: Widget kind, viewId, moduleId
widget.mapKind("mywidget",
    "widgets/mywidget/mywidget",
    "widgets/mywidget/mywidget");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top