#{referencing} UserControls

  1. #1

    #{referencing} UserControls

    Hey, using Coolite for the first time on a project and it's pretty exciting.

    I use UserControls pretty extensively as sort of "pre-configured" Controls (like bundling a Window and it's children in a user control for re-use). But then it's difficult to reference the Ext client objects them from parent controls, especially from a Handler attribute where you can't use <%= Mywindow.ClientID %> idiom.

    One thing I've added to the code on my end is an IClientReferenceProvider interface that provides a hook into the TokenUtils.ReplaceIDTokens() method:


    public static string ReplaceIDTokens(string script, Control seed)
    {
        Regex regex = new Regex(TokenUtils.IDPattern);
        MatchCollection matches = regex.Matches(script);
        Control control = null;
        string id = "";
        foreach (Match match in matches)
        {
            id = match.Value.Remove(match.Value.Length - 1).Remove(0, 2);
    
            control = ControlUtils.FindControl(seed, id);
    
            if (control != null)
            {
                if (control is Observable)
                {
                    script = script.Replace(match.Value, control.ClientID);
                } // ADDED
                else if (control is IClientReferenceProvider)
                { 
                    script = script.Replace(match.Value, ((IClientReferenceProvider)control).GetClientReference());
                } // END ADDED
                else
                {
                    script = script.Replace(match.Value, string.Concat("Ext.get(\"", control.ClientID, "\")"));
                }
            }
            else
            {
                script = script.Replace(match.Value, string.Concat("Ext.get(\"", match.Value.Remove(match.Value.Length - 1).Remove(0, 2), "\")"));
            }
        }
    
        return script;
    }
    and then, UserControls implementing IClientReferenceProvider can either return the ID of the main container control or even return a reference to a custom object that provides a client interface to parent objects like:

    MyUserControl.ascx

    <script type="text/javascript">
    <%= ClientID %> = {
    
    show: function(activityID) { 
    <%= Store1.ClientID %>.baseParams.activityID = activityID;
            <%= Store1.ClientID %>.load();
            <%= Window1.ClientID %>.show();
    }
    };
    </script>
    MyUserControl.ascx.cs

    public class MyUserControl : UserControl, IClientReferenceProvider
    {
    public string GetClientReference() {
    return ClientID;
    }
    }
    It's not 100% elegent but it's proved useful in UserControls that build heavily on existing controls with minimal additional logic. What do you think? Maybe I'm altogether missing a better way of doing this...?
  2. #2

    RE: #{referencing} UserControls

    Hi abertram,

    Thanks for the feedback!

    One control we're been experimenting with for a while now is the <ext:TokenScript>, which enables you to add script tokens to be parsed from inside a standard <script> block.

    Example

    <ext:TokenScript runat="server">
        <script type="text/javascript">
            var doSomething = function () {
                #{TextField1}.setValue('Hello World!');
            }
        </script>
    </ext:TokenScript>
    The above token (#{TextField1}) is basically the same as writting <%= TextField1.ClientID %>

    You might find this helpful, although I'm not 100% sure it was included with the v0.7 release or whether it will be included in a future release. We also renamed from <ext:TokensScript> to <ext:TokenScript>.

    As mentioned above, we're still experimenting with this control, but any feedback you can provide would be helpful.

    Geoffrey McGill
    Founder

Similar Threads

  1. Replies: 6
    Last Post: Aug 13, 2013, 5:59 AM
  2. [CLOSED] Referencing page controls in JavaScript objects
    By PLoch in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Aug 16, 2011, 5:59 PM
  3. [CLOSED] [1.0] Referencing User Controls using #{}
    By bsnezw in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Jan 15, 2010, 10:59 AM
  4. Replies: 4
    Last Post: May 21, 2009, 12:50 PM
  5. [CLOSED] Gray theme css referencing default folder
    By jchau in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Nov 05, 2008, 8:08 PM

Posting Permissions