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):
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.
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..
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.
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.
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.
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 :-)
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):
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!
Have a look at the source of uComponents. They use it a lot I think.
http://ucomponents.codeplex.com/SourceControl/list/changesets
Jeroen
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
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
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:
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.
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.
You might also need to add a loader control to your page/uc - check the wiki for more info
-Tom
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
is working on a reply...