What’s the current best practice for a Xojo Web (2.0) app to return data to the server? Specifically, I’d like to be able to use
WebSession.executeJavascript("...")
and have the result come back into the WebSession instance.
I see several threads about this, but some of them are a mix of Web 1 and Web 2, and some of them contain ‘do it this way’ with later comments saying ‘don’t do it this way’.
E.g. I see recommendations saying:
this.triggerServerEvent()
or
XojoWeb.session.comm.triggerServerEvent()
or
comm.triggerServerEvent()
Other suggestions are to use the WebSDK which looks more complicated than I want, as the WebSDK would requires me to add a new Control to every WebPage.
I just want the data to be returned to the WebSession instance.
I don’t think there is a specific API for that, so this is a bit hacky, undocumented and could change in the future… but… if you want to avoid the Web SDK, maybe you can play with WebSession’s RecordData, RemoveData and RequestData.
The JS way to use “RecordData” is by calling XojoWeb.session.storage.setItem in JavaScript. So, for example, getting the current time:
ExecuteJavaScript("XojoWeb.session.storage.setItem('foo', new Date().getTime())")
Session.RequestData("foo")
You will receive a RequestedData event with foo as the key, and the current time from the browser.
There’s a technique that I shared late last year that might give you some ideas on how to return data to the correct Web session. Essentially, it involves sending requests back to the app, and the requests get handled via the HandleURL event handler. The request includes the user’s session ID, which is used to get a WebSessionContext. From there, you can pretty much do what you want.
I found another way of doing it which is super duper hacky:
Running this code in the Session: self.ExecuteJavaScript("syntaxError12345678")
Will fail, and trigger WebSession.JavaScriptError() event. The error is Can't find variable: syntaxError12345678
From this, you could extract the 12345678 from the error string.
I do not recommend using this for real.
However, since Xojo already has the code to send back the JavaScriptError() event, it seems likely that it wouldn’t be a large change to send back some sort of JavascriptResults() event in the same fashion.
The concept of WebStyle still exists. If you don’t need to modify your bootstrap theme, you can modify controls individually or use a common webstyle to make them consistent.