How to get actual data from e
Hello,
I am fetching some data in app.js from my webserver and send to my webview using fireevent.but in web view how that data is to be fetched frome e.
Code App.js:
Titanium.App.fireEvent("some_event_name",this.responseText);
Code webview.html:
<script>
Titanium.App.addEventListener("some_event_name",function(e)
{
alert("hi"+e.data);
});
</script>
how that data is to be fetched?
2 Answers
-
Accepted Answer
You'll pass an object to fireEvent and then access it's properties in e, like this:
app.js
Titanium.App.fireEvent("some_event_name", { res: this.responseText });
webview.html
<script type="text/javascript"> Titanium.App.addEventListener("some_event_name",function(e) { alert("hi"+e.res); }); </script>
Hope that helps
-
Hi
assuming this.ResponseText contains the data you need
Try this
var webFormData = {}; webFormData.Data = this.ResponseText; Titanium.App.fireEvent('webForm_DataIsAvailable', webFormData);
and in your web form
// Accept Data Ti.App.addEventListener('webForm_DataIsAvailable', webFormDataIsAvailableHandler); function webFormDataIsAvailableHandler(e) { var data = e.Data; }
The reason i pass in a function pointer ie webFormDataIsAvailableHandler as opposed to the function(e) {} inline function is that its useful for cleaning up the handler at a later stage. using
Ti.App.removeEventListener('webForm_DataIsAvailable', webFormDataIsAvailableHandler);
I also you jQuery in my webform and have it fire an event to let the window know it is actually ready for the data first so something like this.
$(function() { Ti.API.info('[WebForm] jQuery Ready'); // Request Data Titanium.App.fireEvent('webForm_ReadyForData'); });
This is just to make sure the DOM is ready for the changes you want to make, but it may be overkill for your situation