Copied to clipboard

Flag this post as spam?

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


  • Douglas Ludlow 210 posts 366 karma points
    Mar 27, 2012 @ 23:20
    Douglas Ludlow
    0

    uComponents doesn't use Client Dependency?

    I'm attempting to develop a time picker data type, but I'm running into an issue with conflicting javascript dependencies. I thought I could try to use Client Dependency to overcome that, however, I've been unsuccessful at getting that to work... at all. So I thought I'd take a look at the uComponents source to see how its being done there because as far as I know, uComponents uses CD.

    But looking at the source, it appears that uComponents injects the resource dependencies directly into the head of the html document and doesn't use CD at all... Am I reading this wrong?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Mar 27, 2012 @ 23:58
    Lee Kelleher
    1

    Hi Douglas,

    You are right, we don't (currently) use ClientDependency in uComponents.  The reason behind this was a bug with CD that didn't allow you to register embedded resources (which is how we deliver all CSS/JavaScript in uComponents).

    I believe this bug has been fixed in CD, but since that we still support Umbraco v4.5.2, the version of CD that shipped with it still has the bug. Hence why we inject the script tags directly into the <head>.

    With the next major release of uComponents (v4.0), we'll be raising the support to Umbraco v4.7+, so hopefully we can use CD.

     

    If you want to see an example of a package using CD, take a look at this code in the source of the Google Maps package.

    Cheers, Lee.

  • Douglas Ludlow 210 posts 366 karma points
    Mar 28, 2012 @ 21:02
    Douglas Ludlow
    0

    Hi Lee,

    Thanks for your reply. I thought I was going crazy yesterday. Thanks for the source code for the Google Maps package. I copied and pasted the code into my project, but it doesn't appear to be adding it to CD. Any thoughts as to what I'm missing?

    using System;
    using System.Web.UI;
    using ClientDependency.Core;
    using ClientDependency.Core.Controls;
    using umbraco;

    namespace CoB.Umb.DataTypes.TimePicker
    {
    public static class TimePickerExtensions
    {
    public static void AddResourceToClientDependency(this Control ctl, string resourceName, ClientDependencyType type)
    {
    ctl.Page.AddResourceToClientDependency(typeof(TimePickerExtensions), resourceName, type, 100);
    }

    public static void AddResourceToClientDependency(this Page page, Type resourceContainer, string resourceName, ClientDependencyType type, int priority)
    {
    // get the urls for the embedded resources
    var resourceUrl = page.ClientScript.GetWebResourceUrl(resourceContainer, resourceName);
    ClientDependencyLoader.Instance.RegisterDependency(priority, page.Server.HtmlEncode(resourceUrl), type);
    }
    }
    }

     

    using ClientDependency.Core;
    using ClientDependency.Core.Controls;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using umbraco.interfaces;
    using System.Web;

    [assembly: WebResource("CoB.Umb.DataTypes.TimePicker.Styles.cob.timepicker.css", "text/css", PerformSubstitution=true)]
    [assembly: WebResource("CoB.Umb.DataTypes.TimePicker.Scripts.jquery-ui-timepicker-addon.js", "application/x-javascript")]
    [assembly: WebResource("CoB.Umb.DataTypes.TimePicker.Scripts.cob.timepicker.js", "application/x-javascript")]
    namespace CoB.Umb.DataTypes.TimePicker
    {
    class TimePickerDataEditor : Panel
    {
    ...

    #region Panel Overrides

    protected override void OnLoad(EventArgs e)
    {
    base.OnLoad(e);

    ...

    this.AddResourceToClientDependency("CoB.Umb.DataTypes.TimePicker.Styles.cob.timepicker.css", ClientDependencyType.Css);
    this.AddResourceToClientDependency("CoB.Umb.DataTypes.TimePicker.Scripts.jquery-ui-timepicker-addon.js", ClientDependencyType.Javascript);
    this.AddResourceToClientDependency("CoB.Umb.DataTypes.TimePicker.Scripts.cob.timepicker.js", ClientDependencyType.Javascript);
    }

    #endregion
    }
    }

     

     

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Mar 28, 2012 @ 22:06
    Lee Kelleher
    0

    Sorry, I have to ask the obvious question... have you set the CSS/JS files to be "Embedded Resource" in the properties (in Visual Studio)?

    The rest of the code looks like it should work.

    Cheers, Lee.

  • Douglas Ludlow 210 posts 366 karma points
    Mar 28, 2012 @ 22:45
    Douglas Ludlow
    0

    Indeed, I have... Oh well, I'll just use ScriptManager for now I guess. It doesn't give me the compression, but atleast it gets them there. This is what I'm doing for now, until I figure out what I'm doing wrong:

    string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "CoB.Umb.DataTypes.TimePicker.Styles.cob.timepicker.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "cob.timepicker.css", css, false);
    ScriptManager.RegisterClientScriptResource(this.Page, this.GetType(), "CoB.Umb.DataTypes.TimePicker.Scripts.jquery-ui-timepicker-addon.js");
    ScriptManager.RegisterClientScriptResource(this.Page, this.GetType(), "CoB.Umb.DataTypes.TimePicker.Scripts.cob.timepicker.js");
  • Douglas Ludlow 210 posts 366 karma points
    Jul 19, 2012 @ 20:56
    Douglas Ludlow
    1

    I had issues using ScriptManager to register my resources the other day. What I found was that for some reason I had to use typeof(CurrentClassName) instead of this.GetType():

    [assembly: WebResource("MyNameSpace.MyCss.css", "text/css")]
    [assembly: WebResource("MyNameSpace.MyJs.js", "text/javascript")]
    namespace MyNameSpace
    {
    class MyClass
    {
    ...

    private void MyMethod()
    {
    string url = Page.ClientScript.GetWebResourceUrl(typeof(MyClass), "MyNameSpace.MyCss.css");
    string css = string.Format("<link href=\"{0}\" type=\"text/css\" rel=\"stylesheet\" />", url);

    ScriptManager.RegisterClientScriptBlock(Page, typeof(MyClass), "MyKey", css, false);
    ScriptManager.RegisterClientScriptResource(Page, typeof(MyClass), "MyNameSpace.MyJs.css");
    }
    }
    }

    I wasn't aware of it, but I guess there's a difference between typeof and GetType, one is the type at compile time, the other is the type at runtime: Type Checking: typeof, GetType, or is?

Please Sign in or register to post replies

Write your reply to:

Draft