Including Javascripts from code

Topics: Customizing Orchard
Jul 7, 2014 at 2:51 PM
Edited Jul 7, 2014 at 2:51 PM
We have a number of contentparts and whilst discussing overriding the view in the theme with the designers it became clear that we should include and require the javascripts from the code. The reason is that we embed a version number in the file name and the developers don't necessarily know whether the view has been overridden in the theme.

We thought that we do this in the driver.Display so we instantiated it the IResourceManager and then added the following to the display method

   _resourceManager.Include("text/javascript", "filename.js",  "filename.min.js");
`` The code runs, but there are no script tags in the html, what are we doing wrong?
Jul 7, 2014 at 5:34 PM
This is because your code runs after the scipts are included. If you include foot scripts those should work but head scripts may not. Instead you can include scripts from e.g. an IResultFilter, from ResultExecuting().
Jul 9, 2014 at 11:50 AM
That worked a treat and there was a good example in the Orchard.MediaLibrary.

The only point left to clear up is the filter runs for each request, I can see that I can filter by url, what I really need to do is detect whether MyContentPart is in the Result and then return specific scripts, how would I do that?
Jul 10, 2014 at 6:10 AM
For that you would need to know whether the current URL is serving up a content item. If you can access the current content item, then you can see if it has MyContentPart attached. I wrote a little class called CurrentContentAccessor ( that you could probably inject into your filter and perform the logic you need.
Jul 11, 2014 at 8:30 AM
That's really interesting. I think I've found an alternate approach to my problem which avoids the need to interrogate every request.

Our issue is version numbers on scripts files changing so we are going to use DefineScript to give our file a name and then change the view from using an Include a specific script file to requiring the named script much cleaner and lets the developer update the script file in code.

Thanks for the input.