Copied to clipboard

Flag this post as spam?

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


  • Max 80 posts 437 karma points
    May 08, 2017 @ 15:09
    Max
    0

    partial view macros - custom javascript widget

    Is there a best practice or better way to include jQuery into a partial view macro?

    We bottom load our jQuery and custom scripts in a "master template layout" so I don't want to include the custom script until the particular partial view macro is rendered.

    In the template view I would have used @section... but this isn't exposed at the partial view level.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    May 08, 2017 @ 15:15
    Alex Skrypnyk
    0

    Hi Max

    I would say that this is more js issue than Umbraco.

    In jQuery you can use some lazy loading of scripts with some statements if there are something on the page.

    Thanks,

    Alex

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    May 08, 2017 @ 15:22
    Nicholas Westby
    0

    This is how I did it in ASP.NET Web Forms: https://www.codeproject.com/Articles/196727/Managing-Your-JavaScript-Library-in-ASP-NET

    It seems like you're using MVC, and I have no idea if that approach works with MVC. However, if you could implement something similar. See here: http://stackoverflow.com/questions/407902/set-global-variable-for-duration-of-a-request

    Essentially, the current HTTP request has an Items property that is a dictionary that only lives for the duration of the current HTTP request. You can add a script reference to that dictionary, then render it out in your "master template layout" if it is present in that dictionary.

    In an Umbraco CSHTML file that inherits UmbracoTemplatePage, you should be able to access the items via this.Context.Items.

  • Max 80 posts 437 karma points
    May 08, 2017 @ 19:54
    Max
    100

    Thanks,

    Figured there was a straight forward way to include a resource that a PVM would require...

    I'll probably reorganize my DOM to top load my scripts for the time being...

    Max

Please Sign in or register to post replies

Write your reply to:

Draft