[CLOSED] [#492] Direct Method scope option not working?

  1. #1

    [CLOSED] [#492] Direct Method scope option not working?

    Hi,

    (This applies to 1.x too)

    In the Direct Methods overview, https://examples2.ext.net/#/Events/D...hods/Overview/ it says that you can provide various options which you list, or the options described in the Sencha documentation at: http://docs.sencha.com/ext-js/4-1/#!...method-request

    Couple of minor things to get out of the way first (both also apply to 1.x):
    1. The link to the sencha docs opens in place - did you intend that or want it outside of the overview tab/iframe?
    2. There isn't mention of the "complete" callback in your doc or the sencha docs - I didn't look at the source code but assuming it is not a sencha feature it may be a very useful feature worth documenting on that overview page?

    More importantly, the sencha doc describes the scope option/property as "The scope in which to execute the callbacks". The means the callback, as well as the specific success and failure callbacks, I believe (and I guess it should apply to the complete call back too)

    I thought this is quite useful in some of my scenarios so gave it a go and found that scope wasn't working. Here is a contrived example:

    <%@ Page Language="C#" %>
    <script runat="server">
        [DirectMethod]
        public string ShowServerTime()
        {
            return DateTime.Now.ToString();
        }
    </script>
    <!DOCTYPE html>
    <html>
        <head>
            <title>Example</title>
            <script>
                var myInstance,
                    MyObject = function () {
                    this.messages = ["Initialized"];
                };
    
                MyObject.prototype.addMessage = function(msg) {
                    this.messages.push(msg);
                };
    
                MyObject.prototype.showMessages = function () {
                    console.log(this.messages);
                };
    
                myInstance = new MyObject();
    
                function getServerTime() {
                    App.direct.ShowServerTime({
                        success: function (result) {
                            myInstance.addMessage('got server time: ' + result);
                        },
                        complete: function () {
                            myInstance.showMessages();
                        }
                    });
                }
    
                function getServerTimeWithThisScope() {
                    App.direct.ShowServerTime({
                        success: function (result) {
                            this.addMessage('got server time: ' + result);
                        },
                        complete: function () {
                            this.showMessages();
                        },
                        scope: myInstance
                    });
                }
            </script>
        </head>
        <body>
            <ext:ResourceManager runat="server" />
            
            <ext:Button runat="server" Text="Get server time">
                <Listeners>
                    <Click Fn="getServerTime" />
                </Listeners>
            </ext:Button>
            
            <ext:Button runat="server" Text="Get server time using this scope">
                <Listeners>
                    <Click Fn="getServerTimeWithThisScope" />
                </Listeners>
            </ext:Button>
        </body>
    </html>
    In the example, I have two buttons. The first one works because it has success/complete handlers that call a method on a defined instance of an object. In the second example, I use the scope of the callbacks as myInstance:

                function getServerTimeWithThisScope() {
                    App.direct.ShowServerTime({
                        success: function (result) {
                            this.addMessage('got server time: ' + result);
                        },
                        complete: function () {
                            this.showMessages();
                        },
                        scope: myInstance
                    });
                }
    In this scenario, I get an error saying this.addMessage is not a function. The scope of this remains as before and doesn't seem to change.

    Did I not configure it right or is this indeed a bug?

    (My example is probably not a great example of where I'd use it, admittedly, but it is more within complex components where a component gets/refreshes a part of itself from the server, and the scope of the callback is ideally the scope of the component, to trigger related activities.)
    Last edited by Daniil; Oct 21, 2014 at 6:23 AM. Reason: [CLOSED]
  2. #2
    Hi @anup,

    Quote Originally Posted by anup View Post
    1. The link to the sencha docs opens in place - did you intend that or want it outside of the overview tab/iframe?
    I used to open such links by a wheel click:) Anyway, thank you for the suggestion. I added target="_blank".

    Quote Originally Posted by anup View Post
    2. There isn't mention of the "complete" callback in your doc or the sencha docs - I didn't look at the source code but assuming it is not a sencha feature it may be a very useful feature worth documenting on that overview page?
    I find that people don't use a complete handler for a DirectMethod. At least, I have not used it ever and not seen someone used that. Maybe, because it is not documented:) Though, I would rather say, because "success" and "failure" handlers are good enough.

    Anyway, your suggestion is a law for us:) I have added a short note about a "complete" option. I updated the online sample. You can review if you want.

    Quote Originally Posted by anup View Post
    More importantly, the sencha doc describes the scope option/property as "The scope in which to execute the callbacks". The means the callback, as well as the specific success and failure callbacks, I believe (and I guess it should apply to the complete call back too)
    Yes, it is more important and complicated. Hopefully, we will get it working for v3.x.
    https://github.com/extnet/Ext.NET/issues/492

    At this point we can suggest to use Ext.Funtion.bind.

    Example
    function getServerTimeWithThisScope() {
        App.direct.ShowServerTime({
            success: Ext.Function.bind(
                        function (result) {
                            this.addMessage('got server time: ' + result);
                        }, 
                        myInstance),
            complete: Ext.Function.bind(
                        function () {
                            this.showMessages();
                        },
                        myInstance)
        });
    }
  3. #3
    Thanks for the Function Bind alternative for now and for updating the examples!

    Personally I do find complete useful as a way to do common cleanup etc regardless of whether it succeeded or failed (e.g. success and fail create different notification options for a Notification window which is shown on complete - just a simple example which I do use a lot as a mini pattern. A few times I use the complete handler for a bit more involved client side processing, though admittedly that is rarer). I do agree that success/failure can be good enough.
  4. #4
    I do agree, a complete handler might be useful. Especially, for advanced developers. We have a few such developers in the Ext.NET world.
  5. #5
    Finally, we got it fixed for Ext.NET v3.0 beta, revision #6067. it should be released soon.

    Now all handlers of a DirectEvent (Before, After, Success, Failure, Complete) and a DirectMethod (success, failure, complete) are being scoped using the "Scope"/"scope" setting if defined.
  6. #6
    That is great. Many thanks!

Similar Threads

  1. Replies: 2
    Last Post: May 13, 2014, 8:52 AM
  2. [CLOSED] How to select a ComboBox option in Direct Method?
    By vadym.f in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Aug 14, 2013, 5:21 PM
  3. [CLOSED] Output Cache issue with Direct Method / Direct Event
    By amitpareek in forum 1.x Legacy Premium Help
    Replies: 18
    Last Post: Mar 01, 2013, 5:03 AM
  4. Replies: 8
    Last Post: Jan 25, 2011, 4:21 AM
  5. [CLOSED] Direct Method only working once
    By CarWise in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jan 07, 2011, 9:09 AM

Posting Permissions