Copied to clipboard

Flag this post as spam?

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


  • aghy 129 posts 308 karma points
    Apr 17, 2011 @ 15:47
    aghy
    0

    Add Javascript to PrevalueEditor

    Is it possible to add a javascript file to a PrevalueEditor?

    I have used the uComponents AddResourceToClientDependency in other places where I have inherited from a control but my PrevalueEditor is inheriting from AbstractJsonPrevalueEditor so I'm not sure if it will work.

    Thanks

    Ben

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 17, 2011 @ 17:05
    Lee Kelleher
    0

    Hi Ben,

    Yes it's definitely possible to use your own JavaScript in the Prevalue Editor.

    Is your JavaScript an embedded resource, or on the file-system?

    If it's an embedded resource, in your own assembly, then you'll need to call the AddResourceToClientDependency override that accepts the resourceContainer Type - as that's the assembly that contains the embedded resource.

    Otherwise if it's on the file-system, then there's no uComponents' method for adding it... so you'll have to roll your own...

    umbraco.library.RegisterJavaScriptFile("uniqueKey", "/path/to/the/javascript.js");

    Cheers, Lee.

  • aghy 129 posts 308 karma points
    Apr 17, 2011 @ 18:11
    aghy
    0

    HI Lee,

    Yes it's an embedded resource, at least I think it is. All I've done is set the Build Action on the js file to Embedded Resource and my code looks like this

    [assembly: System.Web.UI.WebResource("_4Ben.DataTypes.MultiType.js.MultiTypeScripts.js", "text/js")]
    namespace _4Ben.DataTypes.MultiType
    {
    public class MultiTypePrevalueEditor : AbstractJsonPrevalueEditor
    {
    protected override void CreateChildControls()
    {
    base.CreateChildControls();
    this.AddResourceToClientDependency("_4Ben.DataTypes.MultiType.js.MultiTypeScripts.js", ClientDependency.Core.ClientDependencyType.Javascript);
    }
    }
    }

    But I still cant access any of the functions in my js file.

  • aghy 129 posts 308 karma points
    Apr 17, 2011 @ 18:15
    aghy
    0

    I also don't know if "_4Ben.DataTypes.MultiType.js.MultiTypeScripts.js" is the correct resourceName.

    Any help would be much appreciated!

    Thanks

    Ben

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 17, 2011 @ 18:18
    Lee Kelleher
    1

    Hi Ben,

    Yeah, the problem here is that the default method "AddResourceToClientDependency" is trying to look for the embedded resource from within the uComponents assembly... so instead you'd need to specify that you're using a different assembly.

    Try this...

    this.Page.AddResourceToClientDependency(typeof(_4Ben.DataTypes.MultiType.MultiTypePrevalueEditor), "_4Ben.DataTypes.MultiType.js.MultiTypeScripts.js", ClientDependency.Core.ClientDependencyType.Javascript, 100);

    I know it seems very long-winded, the code could be cut-down a little... (maybe using "this.GetType()" for the first parameter?) ... but best to get it working first! ;-)

    Let me know if it works.

    Cheers, Lee.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 17, 2011 @ 18:20
    Lee Kelleher
    0

    ... also, I'm curious what you are developing ... if you can let us know, please do! :-)

    Thanks, Lee.

  • aghy 129 posts 308 karma points
    Apr 17, 2011 @ 19:06
    aghy
    0

    Fantastic! Thanks!

    I must have been using a reference to an old version of uComponents because I didn't have AddResourceToClientDependency with four parameters. And yes this.GetType() works for the first parameter.

    I'm making something very similar to the Embeddable Content datatype but for my own purpose. I started making it ages ago to help with the Pliable Ajax Form project so that I can values and text to a select box. Also I like the way uComponents stores Prevalue data as json.

    Many thanks again for the help!

    Ben

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 17, 2011 @ 19:15
    Lee Kelleher
    1

    You're welcome Ben. Best of luck with your project!

    Next version of uComponents (v2.2) is due out in the next few weeks - but sure to check out the new DataType Grid data-type! :-)

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft