PDA

View Full Version : [CLOSED] [#492] Direct Method scope option not working?



anup
May 23, 2014, 11:29 AM
Hi,

(This applies to 1.x too)

In the Direct Methods overview, http://examples2.ext.net/#/Events/DirectMethods/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/#!/api/Ext.data.Connection-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.)

Daniil
May 23, 2014, 4:27 PM
Hi @anup,



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".



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.



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)
});
}

anup
May 27, 2014, 9:30 AM
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.

Daniil
May 27, 2014, 6:22 PM
I do agree, a complete handler might be useful. Especially, for advanced developers. We have a few such developers in the Ext.NET world.

Daniil
Oct 21, 2014, 7:22 AM
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.

anup
Oct 27, 2014, 5:31 PM
That is great. Many thanks!