Copied to clipboard

Flag this post as spam?

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


  • awm 187 posts 376 karma points
    Feb 09, 2012 @ 17:56
    awm
    0

    ClientDependency and custom user controls

    Hi guys,

    I've just created a custom user control for a project that uses a few js scripts. Usually I just stick the script includes at the top of the user control and make sure they exist in the umbraco installation, but after reading about ClientDependency I'm wondering whether it's possible to embed the scripts within the assembly so I'd no longer have to worry about external dependencies. This is what ClientDependency was designed for? (I think).

    Anyway I can't figure out how to do this. I've already embedded the resources using the following (with jquery as an example):

    [assembly: WebResource("AeternumControls.scripts.jquery-1.7.1.min.js", "text/javascript", PerformSubstitution = true)]

    The only problem is I don't know how to now include these embedded resources in my control? Is it a case of executing some code within the 'OnInit' function? Do I somehow reference the resources in the .ascx file? I'm very confused.

    Is anyone able to explain how this all works?

    Thanks!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 09, 2012 @ 18:09
    Jeroen Breuer
    2

    Have a look at the source of uComponents. They use it a lot I think.

    http://ucomponents.codeplex.com/SourceControl/list/changesets

    Jeroen

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 10, 2012 @ 05:34
    Tom Fulton
    1

    Hi ali,

    IIRC uComponents doesn't use ClientDependency (maybe due to some compatibility issues?).  But you can see in the source if ResourceExtensions.cs they created a method that adds a resource to the header via a literal tag (note I think this requires head runat="server").  And here's an example where they call it.

    If you do want to use ClientDependency though this wiki might help, although not sure how to do it with an embedded resource...maybe by constructing a ClientDependencyInclude and adding that to the header..hmm..

    -Tom

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 10, 2012 @ 09:21
    Jeroen Breuer
    2

    DAMP uses ClientDependency, but doesn't embed the scripts. So the scripts are still separate files, but to me that doesn't really matter because it still uses ClientDependency so it's faster.

    File that includes ClientDependency:
    http://damp.codeplex.com/SourceControl/changeset/view/86116#1765805 

    Script that is used:
    http://damp.codeplex.com/SourceControl/changeset/view/86116#1826730 

    Jeroen 

  • awm 187 posts 376 karma points
    Feb 10, 2012 @ 12:12
    awm
    0

    Hi Tom,

    I had a look at the ResourceExtensions class during my investigation but for some reason I couldn't get it to compile? There was some issue with having:

     publicstaticvoid AddResourceToClientDependency(this Control ctl, ...

    ie. the first parameter "this Control ctl" wasn't being accepted. I copied the class exactly (and included the relevant references) so I'm not sure where I was going wrong?

    Jeroen: I was thinking of doing the same thing, ie. having the scripts as seperate files but I figured this wasn't what I was trying to achieve? I mean ultimately I can get my control to work.. I was mainly using this as a chance to play around with ClientDependency. I was hoping I'd somehow be able to embed the resources and then pull them out in my .ascx file and keep all the code encapsulated within my control.

    Is this even possible or have I misunderstood what ClientDependency's actually used for? I watched the 2011 Codegarden presentation by Shannon where he discusses ClientDependency and he said in the opening statement that it was created to give a control access to all the resources it needs without knowing what it's parent site is using...

    Thanks for the help though guys - I appreciate it. 

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 10, 2012 @ 14:33
    Tom Fulton
    1

    Hi Ali,

    What was the error you got?  Maybe it's because you need to include it in a static class?  Did you get the error when calling it or just compiling the method?

    RE: ClientDependency - you can try something like this, assuming you are embedding using your above code and have set the file as an embedded resource.

    string url = this.Page.ClientScript.GetWebResourceUrl(typeof(YourClass), "YourNamespace.Path.To.YourFile.js");
    ClientDependency.Core.Controls.ClientDependencyLoader.Instance.RegisterDependency(url, ClientDependency.Core.ClientDependencyType.Javascript);

    You might also need to add a loader control to your page/uc - check the wiki for more info

    -Tom

  • awm 187 posts 376 karma points
    Feb 10, 2012 @ 14:42
    awm
    0

    Hi Tom,

    Bizarrely I just tried to re-create the class using the code you linked above and this time it seems to have compiled? Maybe I was using an old version of the code or something.. but when I tried yesterday it wasn't accepting it. 

    Anyway thanks for that! I'll have another stab using this class :-)

    Al

Please Sign in or register to post replies

Write your reply to:

Draft