[AC-2905] HttpClient "bound" callbacks not working
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | n/a |
Status | Closed |
Resolution | Needs more info |
Resolution Date | 2011-12-26T05:41:37.000+0000 |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | Titanium SDK & CLI |
Labels | HTTPClient |
Reporter | Michael Welter |
Assignee | Paul Dowsett |
Created | 2011-11-11T12:26:34.000+0000 |
Updated | 2016-03-08T07:47:52.000+0000 |
Description
Creating an XHR with: var xhr = Titanium.Network.createHTTPClient()
and then setting the callback with: xhr.onreadystatechange = this.func.bind(null, this)
Following the "xhr.send(data)", I observe the proper TCP packets being sent to the server and the proper response coming back from the server. However, immediately following the "xhr.send(data)", the system throws:
E/KrollCallback( 314): (kroll$1: app://app.js) [282,3299] Error, invocation: [callMethod Network.HTTPClient.(anonymous) org.appcelerator.titanium.kroll.KrollCallback@45118288], message: null
E/KrollCallback( 314): java.lang.IllegalStateException
E/KrollCallback( 314): at org.mozilla.javascript.ScriptRuntime.getTopCallScope(ScriptRuntime.java:3159)
E/KrollCallback( 314): at org.mozilla.javascript.BoundFunction.call(BoundFunction.java:82)
E/KrollCallback( 314): at org.appcelerator.titanium.kroll.KrollCallback.callSync(KrollCallback.java:139)
E/KrollCallback( 314): at org.appcelerator.titanium.kroll.KrollCallback$1.run(KrollCallback.java:164)
E/KrollCallback( 314): at android.os.Handler.handleCallback(Handler.java:587)
E/KrollCallback( 314): at android.os.Handler.dispatchMessage(Handler.java:92)
E/KrollCallback( 314): at android.os.Looper.loop(Looper.java:123)
E/KrollCallback( 314): at org.appcelerator.titanium.kroll.KrollHandlerThread.run(KrollHandlerThread.java:86)
If I remove the bind from the callback function then it works as it should (except that I don't have "this" in the parameter list of the callback.
The software works as it should on the iPhone simulator.
The following workaround seems to solve the problem: Add the following member to Function: Function.prototype.prependArg = function (arg) { var func = this; return function () { var newargs = [arg]; for (var i = 0; i < arguments.length; i++) { newargs.push(arguments[i]); } return func.apply(this, newargs); }; }; Change the xhr callback to: xhr.onreadystatechange = this.func.prependArg(this);
Thanks for raising this ticket. In order for me to escalate it to the core team, all the fields must be complete. Please check it against the [JIRA Ticket Checklist](https://wiki.appcelerator.org/display/guides/How+to+Submit+a+Bug+Report#HowtoSubmitaBugReport-JIRATicketChecklist), and add any missing information. Furthermore, a test case must run without modification, as per the [Creating a Test Case](https://wiki.appcelerator.org/display/guides/How+to+Submit+a+Bug+Report#HowtoSubmitaBugReport-CreatingaTestCase) section. I will close this for now. Please reopen it once it is complete, and I will move it to the main project. Cheers
Closing due to inactivity. If this issue still exists, please raise a new ticket, including all the information in the [JIRA Ticket Checklist](https://wiki.appcelerator.org/display/guides/How+to+Submit+a+Bug+Report#HowtoSubmitaBugReport-JIRATicketChecklist) to ensure that we can escalate it quickly. Read [How to Submit a Bug Report](https://wiki.appcelerator.org/display/guides/How+to+Submit+a+Bug+Report) if you have not read it before, and always start a ticket using the [JIRA Ticket Template](https://wiki.appcelerator.org/display/guides/JIRA+Ticket+Template). Thanks in advance