[CLOSED] Revisting Updates to Listeners and DirectEvents inside DirectEvents.

Page 1 of 2 12 LastLast
  1. #1

    [CLOSED] Revisting Updates to Listeners and DirectEvents inside DirectEvents.

    We talked about something related to this here: http://forums.ext.net/showthread.php...in-code-behind


    So I'm revisiting this because I'm finding a number of deficiencies how I can change/update/remove listeners in a DirectEvent using server-side code.

    We addressed remove, sorta. I still think you should review my suggestions at the end of that thread. I see no reason why ext.net maintains such a rich set of enumerators of events for each control if we can't refer to them directly to construct the javascript for us. Example. Button1.Listeners.Click.Remove(); Instead, presently, to remove a listener, I have to call X.AddScript( Button1.ClientID + ".removeListener( '" + listenername + "');"; This goes for all listener commands including suspend, resume, on, etc. This goes for the DirectEvent events too. Everywhere else inside Ext.net, you do a great job of resolving property setting controls between initialization and DirectEvent javascript construction commands. You did add RemoveDirectListener, but again, we need to reference by name the eventname. It's not a part of the Event Enumeration of DirectEvents.

    Then we come to add. The only way to add a listener in a DirectEvent is using On, which is back to naming the event. I guess this allows us to attach more than one event to a single listener name. This will at least allow us to add a listener during a DirectEvent at least. But how do we add a DirectEvent in a DirectEvent?

    Then there's update. Not even present or possible in a DirectEvent.

    I have a case where I have a button with a listener in the presentation layer with a handler which contains a format string, example: Handler="document.location='{0}'; I do a lot of format or replaces of the {0} with urls that have to be constructed. This works great as long as I update it in the initial Page_Load lifecycle. But I have a case where I can't do that till the DirectEvent. I guess one could argue it's probably unwise to set up a format string that is being rendered to the initialized listener; perhaps it would be better to do an Add later. But in this case, my button is disabled or hidden so no risk of being clicked. Let me argue my case further, let's say I want to prepend or append javascript the existing handler in the DirectEvent without changing what's already present? I do this at times. At this point, the only way to do this is remove the listener then add.

    So I'm asking for a fuller library of add/replace/update/delete/suspend/resume/etc set of functions on the serverside for the base of ComponentListener and ComponentDirectEvent that are able to create javascript for the client in a DirectEvent in the same way I can edit other properties of a control in a DirectEvent. Also each should support Clear or RemoveAll.

    These should match the set of commands found as methods available for listeners here: http://docs.sencha.com/extjs/4.2.1/#...til.Observable and matched to support equivalently to directevents.
    Last edited by Daniil; Jan 13, 2014 at 5:34 AM. Reason: [CLOSED]
  2. #2
    Hello!

    Interesting points. We will discuss your points in this and other threads.

    Thank you!
  3. #3
    I'm struggling still with doing something as simple as replacing a listener in a DirectEvent. Some guidance would be helpful. I just need to change the listener on a button in a direct event. The problem is because this button's listener is always set in the DirectEvent, I can't simply call RemoveListener because it requires the function to be passed in to match. I can't reconstruct it to remove it from the DirectEvent. I need a general way to remove or replace. Thoughts?
  4. #4
    To correctly remove a listener you need to use 'removeListener/un' javascript method
    http://docs-origin.sencha.com/extjs/...removeListener

    You have to pass event name, function reference (which was used when you add listener) and scope (if scope was passed to addListener)

    When you register event handler using Handler
    Button1.Listeners.Click.Handler = "alert('click');";
    Then anonymous function is created, like
    function () {alert('click');}
    So, we cannot remove click listener if you use Handler because we have no function reference.
    There is 'clearListeners' method but I strongly suggest do not use them because widget can register own event handlers (for internal use) and clearListeners method can crash widget logic. clearListeners must be use during destroy only

    You need to use Fn instead Handler if you need to have ability to deattach event handlers
    See the following sample
    <%@ Page Language="C#" %>
     
     
    <script runat="server">
        protected void Page_Load( object sender, EventArgs e ) {
        }
        protected void OnClick( object sender, DirectEventArgs e ) {
            ButtonB.Un("click", "alertA");
            ButtonB.On("click", "alertB");
        }
    </script>
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
        <title></title>
        <script>
            function alertA () {
                alert('A');
            }
    
    
            function alertB () {
                alert('B');
            }
        </script>
    </head>
    <body>
        <form id="Form1" runat="server">
            <ext:ResourceManager runat="server" />        
            
            <ext:Button ID="ButtonB" runat="server" Text="Alert">
                <Listeners>
                    <Click Fn="alertA" />
                </Listeners>
            </ext:Button>
    
    
            <ext:Button ID="ButtonA" runat="server" Text="Change Handler" OnDirectClick="OnClick" />            
           
        </form>
    </body>
    </html>
    Another way, to have one handler only but its logic can be changed
    <%@ Page Language="C#" %>
     
     
    <script runat="server">
        protected void Page_Load( object sender, EventArgs e ) {
        }
        protected void OnClick( object sender, DirectEventArgs e ) {
            ButtonB.Set("currentLogic", JRawValue.From("alertB"));
        }
    </script>
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
        <title></title>
        <script>
            function baseHandler (button) {
                if (button.currentLogic) {
                    button.currentLogic.call();
                }
            }
            
            function alertA () {
                alert('A');
            }
    
    
            function alertB () {
                alert('B');
            }
        </script>
    </head>
    <body>
        <form id="Form1" runat="server">
            <ext:ResourceManager runat="server" />        
            
            <ext:Button ID="ButtonB" runat="server" Text="Alert">
                <Listeners>
                    <Click Fn="baseHandler" />
                </Listeners>
                <CustomConfig>
                    <ext:ConfigItem Name="currentLogic" Value="alertA" Mode="Raw" />
                </CustomConfig>
            </ext:Button>
    
    
            <ext:Button ID="ButtonA" runat="server" Text="Change Handler" OnDirectClick="OnClick" />            
           
        </form>
    </body>
    </html>

    or

    <%@ Page Language="C#" %>
     
     
    <script runat="server">
        protected void Page_Load( object sender, EventArgs e ) {
        }
        protected void OnClick( object sender, DirectEventArgs e ) {
            ButtonB.Set("clickMessage", "B");
        }
    </script>
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
        <title></title>    
    </head>
    <body>
        <form id="Form1" runat="server">
            <ext:ResourceManager runat="server" />        
            
            <ext:Button ID="ButtonB" runat="server" Text="Alert" Handler="alert(this.clickMessage);">            
                <CustomConfig>
                    <ext:ConfigItem Name="clickMessage" Value="A" />
                </CustomConfig>
            </ext:Button>
    
    
            <ext:Button ID="ButtonA" runat="server" Text="Change Handler" OnDirectClick="OnClick" />            
           
        </form>
    </body>
    </html>

    Also, you can simply to change button Handler because button uses schema from second example internally
    <%@ Page Language="C#" %>
     
     
    <script runat="server">
        protected void Page_Load( object sender, EventArgs e ) {
        }
        protected void OnClick( object sender, DirectEventArgs e ) {
            ButtonB.Handler = "function () { alert('B');}";
        }
    </script>
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
        <title></title>    
    </head>
    <body>
        <form id="Form1" runat="server">
            <ext:ResourceManager runat="server" />        
            
            <ext:Button ID="ButtonB" runat="server" Text="Alert" Handler="alert('A');" />
    
    
            <ext:Button ID="ButtonA" runat="server" Text="Change Handler" OnDirectClick="OnClick" />            
           
        </form>
    </body>
    </html>
  5. #5
    Vladimir, this is an excellent post. I think this is the first tour I've seen of all the options to update listeners in postback. Thank you.

    Though, don't you think that your last example is a bit strange that to edit the Handler string in Page_Load is different than is required in postback? It seems to me you should wrap the .Handler string in a JFunction in DirectEvents just as you do in Page_Load. Seems the inconsistency is something that should be fixed?
    Last edited by michaeld; Dec 01, 2013 at 10:48 AM.
  6. #6
    I think the best way is to do :

    protected void OnClick( object sender, DirectEventArgs e ) {
            Button.Listeners.Click.Handler="what you want" + somevariabe;
            Button.Render()
        }
    Hope it help
  7. #7
    Quote Originally Posted by jamalmellal View Post
    I think the best way is to do :

    protected void OnClick( object sender, DirectEventArgs e ) {
            Button.Listeners.Click.Handler="what you want" + somevariabe;
            Button.Render()
        }
    Hope it help
    Thanks jamalmellal. Yeah, I've found this way has been the most reliable for the most consistency between Page_Load and DirectEvent/Method variations. Though, it's no good for Container and Panel Listners if you don't want to also blow away the client-side children values.
  8. #8
    Quote Originally Posted by michaeld View Post
    Though, don't you think that your last example is a bit strange that to edit the Handler string in Page_Load is different than is required in postback? It seems to me you should wrap the .Handler string in a JFunction in DirectEvents just as you do in Page_Load. Seems the inconsistency is something that should be fixed?
    You are right. Thank you for the suggestion. We even consider it a bug. Fixed in revision #5602. It will go to v2.5.
  9. #9
    I looked at the change you made in the SVN and ran a search for other cases of SetHandler in Ext.Net. It appears you may need to make the same change to MenuItemBase.cs as well.
  10. #10
    Thank you! Somehow this idea to search for SetHandler didn't pop up in my head:)

    Besides MenuItemBase, there are also a Handler in DesktopShortcut and an overloaded ButtonBase's SetHandler taking two parameters - handler and scope.

    Everything is corrected in revision #5637.
Page 1 of 2 12 LastLast

Similar Threads

  1. [CLOSED] ClientID inside DirectEvents
    By krzak in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Mar 01, 2011, 10:32 AM
  2. Replies: 1
    Last Post: Aug 21, 2010, 9:49 AM
  3. Dynamic DirectEvents
    By Dominik in forum 1.x Help
    Replies: 3
    Last Post: Jul 30, 2010, 12:58 PM
  4. DirectEvents are not available at all for any control
    By lordofthexings in forum 1.x Help
    Replies: 3
    Last Post: May 19, 2010, 4:47 PM
  5. [CLOSED] DirectEvents inside a window
    By sharif in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Apr 15, 2010, 11:16 AM

Tags for this Thread

Posting Permissions