Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jurgen Wiersema 5 posts 75 karma points
    Feb 02, 2017 @ 14:51
    Jurgen Wiersema
    0

    Partial View Macro - output script tags

    Hi,

    I'm new to umbraco and want to achieve the following: - I want to be able to create html+js-only widgets. I order for the JS to work it has to be loaded at the bottom of the page after some pre-requisite scripts (jQuery, React, Adal.js etc).

    I have followed this post: http://stackoverflow.com/questions/5355427/populate-a-razor-section-from-a-partial?rq=1 in order to inject script tags from a partial view into the bottom of a masterpage.

    However, the HTML helper in the partial is not being triggered (the one in the masterpage IS being hit). I am wondering if I'm doing something fundamentally wrong or just doing something slightly wrong.

    Here is the Partial view macro

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using JW.Umbraco.Second.Extensions
    
    <div class="ms-font-su ms-fontColor-white" id="userNameContainer">
        <h2 id="userName">Loading..</h2><div id="userProfilePictureHolder"><i class="ms-Icon ms-Icon--person"></i></div>
    </div>
    
    @Html.RequireScript("/scripts/system.js")
    @Html.RequireScript("/scripts/net.js")
    @Html.RequireScript("/scripts/ui.js")
    @Html.RequireScript("/scripts/App.js")
    

    Master.cshtml

    <!-- Javascripts -->
        <script src="~/js/jquery.min.js"></script>
        <script src="~/js/bootstrap.min.js"></script>
        <script src="~/scripts/fanoe.js"></script>
        <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-with-addons.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.min.js"></script>
        <script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.12/js/adal.min.js"></script>
        <!-- <script src="~/scripts/system.js"></script>
        <script src="~/scripts/net.js"></script>
        <script src="~/scripts/ui.js"></script>
        <script src="~/scripts/App.js"></script> -->
        @Html.EmitRequiredScripts()
      </body>
    </html>
    

    This is the HTML helper from the stckexchange post people have used successfully:

    public static string RequireScript(this HtmlHelper html, string path, int priority = 1)
    {
        var requiredScripts = HttpContext.Current.Items["RequiredScripts"] as List<ResourceInclude>;
        if (requiredScripts == null) HttpContext.Current.Items["RequiredScripts"] = requiredScripts = new List<ResourceInclude>();
        if (!requiredScripts.Any(i => i.Path == path)) requiredScripts.Add(new ResourceInclude() { Path = path, Priority = priority });
        return null;
    }
    
    public static HtmlString EmitRequiredScripts(this HtmlHelper html)
    {
        var requiredScripts = HttpContext.Current.Items["RequiredScripts"] as List<ResourceInclude>;
        if (requiredScripts == null) return null;
        StringBuilder sb = new StringBuilder();
        foreach (var item in requiredScripts.OrderByDescending(i => i.Priority))
        {
            sb.AppendFormat("<script src=\"{0}\" type=\"text/javascript\"></script>\n", item.Path);
        }
        return new HtmlString(sb.ToString());
    }
    public class ResourceInclude
    {
        public string Path { get; set; }
        public int Priority { get; set; }
    }
    

    Please let me know if this is a good way to go, what I'm doing wrong or if there are superior ways to solve this problem.

    Kind Regards, Jurgen

  • Jurgen Wiersema 5 posts 75 karma points
    Feb 03, 2017 @ 08:27
    Jurgen Wiersema
    0

    NB: I've noticed that when in the backoffice the method RequireScript IS called, but not EmitRequiredScripts. This is logical because EmitRequiredScripts is in the front-end masterpage. However, RequireScript is not being called in the front-end. It seems to be stripped out somehow. Similarly when I add

Please Sign in or register to post replies

Write your reply to:

Draft