Extending/overriding scripts

Topics: Writing modules
Developer
Feb 7, 2012 at 6:38 PM

I wonder if there's a way for this without custom coding:

In a module there's a script (defined in a ResourceManifest) included. I'd like to modify the behaviour of this script with a new script, without completely overriding it, with jQuery.extend() (overriding only methods).

But how to include the new script file along the original one without overriding the shape template where the inclusion (Require()) call is written? Redefining the resource seems to have no effect, but also overriding the script file completely just like shape templates has no effect either.

Thanks in advance.

Coordinator
Feb 7, 2012 at 6:50 PM

Why not include your additional script, from a handler or shape table provider, or filter, depending on what it is you are trying to do, which is not super-clear from that description: what currently requires the script?

Developer
Feb 8, 2012 at 11:09 AM

How could I forget that, of course a handler is the way to go.

There's a Require() in a (part's) shape template, I wanted to include another script there. Now indeed the solution is to include that another script in a handler, hooked to the content type's BuildDisplay().

Feb 8, 2012 at 1:45 PM

Would you mind posting an example if/when you do this? I'd like to see how to include a script from a handler. 

Developer
Feb 8, 2012 at 2:03 PM

This is the code:

    public class MyHandler : ContentHandler
    {
        private readonly IResourceManager _resourceManager;

        public MyHandler(IResourceManager resourceManager)
        {
            _resourceManager = resourceManager;
        }

        protected override void BuildDisplayShape(BuildDisplayContext context)
        {
            if (context.ContentItem.ContentType != "WhereIWantToIncludeResources") return;

            _resourceManager.Require("script", "ScriptName").AtHead();
            _resourceManager.Require("stylesheet", "StyleName");
        }
    }

(Code insertion still not working.)

You could overrride other methods depending on when you want to include the resources, or you could use the On* methods to use the events of parts (not content items as here).