2

I am aware that this violates the MVC principle and best practices.

I have a whole bunch of custom Angular components that each take a whole bunch of optional parameters and each require a different remote stylesheet and javascript file. I would like to render these with an HtmlHelper without having to manually include the right resources everywhere I use them.

I was hoping that this would do the trick but it doesn't

public static class HtmlExtensions
{
    private static IResourceManager _resourceManager;

    // Executed in the Activated method of an OrchardShellEvents implementation
    public static void SetResourceManager(IResourceManager resourceManager)
    {
        _resourceManager = resourceManager;
    }

    public static MvcHtmlString Angular(this HtmlHelper helper, CustomAngularComponent component)
    {
        // Require the resources
        var _styleRegister = new ResourceRegister(helper.ViewDataContainer, _resourceManager, "Style");
        var _scriptRegister = new ResourceRegister(helper.ViewDataContainer, _resourceManager, "Script");

        _styleRegister.Require(component.StyleSheet).AtHead();

        if (!string.IsNullOrEmpty(component.Script))
        {
            _scriptRegister.Require(component.Script).AtFoot();
        }

        // Create tag
        var tag = new TagBuilder(component.Tag);
        tag.MergeAttributes(component.Parameters);

        return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
    }
}

I could use an arbitrary Shape as a helper, something like:

@Display(New.Angular(Model: new CustomAngularComponent(...)))

but a helper with strongly typed parameters feels a lot better.

Lawyerson
  • 919
  • 1
  • 7
  • 25
  • Try to change `"Style"` to `"style"` and `"Script"` to `"script"`... – ViRuSTriNiTy May 23 '17 at 09:00
  • @ViRuSTriNiTy Thank you for pointing me in that direction. Drilling deep into the `ResourceRegister` showed me that it expects the keys `"stylesheet"` and `"script"` to construct the correct basepath. The files still aren't being included in the page though. Maybe it would be easier to figure out how to return a shape containing the includes from the HtmlHelper instead. – Lawyerson May 26 '17 at 08:14

1 Answers1

0

This comment on another Orchard question tipped me off. As it turns out, simply injecting Work<IResourceManager> and using it directly was enough to make my setup work. Please note that this does not conform to best MVC practices, by doing this I sacrifice maintainability in favor of readability.

public static class HtmlExtensions
{
    private static Work<IResourceManager> _resourceManager;

    // Executed in the Activated method of an OrchardShellEvents implementation
    public static void SetResourceManager(Work<IResourceManager> resourceManager)
    {
        _resourceManager = resourceManager;
    }

    public static MvcHtmlString Angular(this HtmlHelper helper, CustomAngularComponent component)
    {
        // Require the resources
        _resourceManager.Value.Require("stylesheet", component.StyleSheet).AtHead();

        if (!string.IsNullOrEmpty(component.Script))
        {
            _resourceManager.Value.Require("script", component.Script).AtFoot();
        }

        // Create tag
        var tag = new TagBuilder(component.Tag);
        tag.MergeAttributes(component.Parameters);

        return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
    }
}
Lawyerson
  • 919
  • 1
  • 7
  • 25