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.
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.
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.
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
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 viathis.Context.Items
.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
is working on a reply...