Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add source to webView fireEvent #14023

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

m1ga
Copy link
Contributor

@m1ga m1ga commented Apr 19, 2024

Currently the event that is fired from a WebView doesn't contain a source object. This makes it impossible to identify the webview that fired the event.

This PR will add the source the the KrollDict.

Example

var win = Ti.UI.createWindow();
var webView = Ti.UI.createWebView({
	url: 'web/index.html',
	myId: "test"                                                  // <------ custom property 
});
var button = Ti.UI.createButton({
	title: 'From Titanium -> WebView',
});
button.addEventListener('click', function(e) {
	Ti.App.fireEvent('app:fromTitanium', {
		message: 'Event fired from Titanium, handled in WebView'
	});
});
Ti.App.addEventListener('app:fromWebView', function(e) {
	console.log(e.source.myId)                    // <--------------- will show "test"
	alert(e.message);
});
win.add(webView);
win.add(button);
win.open();

HTML file

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, user-scalable=no" />
    <script>
      // Send event from the web-view to the app
      function sendEventToApp() {
      Ti.App.fireEvent('app:fromWebView', {
      message: 'Event fired from WebView, handled in Titanium'
    });
      }

    // Subscribe to global event
      Ti.App.addEventListener("app:fromTitanium", function(e) {
        alert(e.message);
      });
    </script>
  </head>
  <body>
    <button onclick="sendEventToApp()">From WebView -> Titanium</button>
  </body>
</html>

Click on the "From WebView -> Titanium" button

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant