May 23, 2014, 10:29 AM
[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:
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.)
(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]