How to fire an event in a Module and catch it in JS
Hi Guys,
i working with modules.
I want to fire an event in the Module and catch the event with an eventListener in JS.
Here is my Module snippet:
-(id)example:(id)args
{
// example method
[self _fireEventToListener:@"my_event" withObject:nil listener:nil thisObject:nil];
return @"hello world";
}
Here is how i bind and call the event in JS:
var inappModule = Titanium.InApp;
inappModule.addEventListener('my_event', function(e)
{
Ti.API.info("Mein Event fired");
});
Ti.API.info("Call Method" + inappModule.example(true,true));
But the Event isnt fired.
Any ideas?
5 Answers
-
Within module try:
[super fireEvent:@"my_event"];
TiModule is a subclass of TiProxy which provides the following fireEvent methods:
-(void)fireEvent:(id)args; -(void)fireEvent:(NSString*)type withObject:(id)obj; -(void)fireEvent:(NSString*)type withObject:(id)obj withSource:(id)source; -(void)fireEvent:(NSString*)type withObject:(id)obj withSource:(id)source propagate:(BOOL)yn; -(void)fireEvent:(NSString*)type withObject:(id)obj propagate:(BOOL)yn;
-
push
-
any idea How to do this on android ?
-
Hi in iOS fire event like this,
-(id)example:(id)args // this example could be any normal method but generally it is a delegate when we fire event. { [self fireEvent:@"my_event" withObject:nil]; or [self fireEvent:@"my_event" withObject:your object to be sent to JS code]; }
in android we do it like this:
public void onError(Object obj1, error err1) { KrollDict kd = new KrollDict(); // put your values in Kroll dict and fire event like this. this.proxy.fireEvent(didFinishWithError, kd); from listener class which implements an interface or this.fireEvent(didFinishWithError, kd); // from with in proxy class }
-
Fire an event from your module in Objective-C:
- (void)fireEvent:(NSNotification *)notification { NSLog(@"firing from iOS!"); [self fireEvent:@"yourNotificationName" withObject:notification.userInfo]; }
listen for it in Javascript:
var module = require('com.YourModule.WhateverYouCallit'); module.addEventListener("yourNotificationName", function(e) { alert("Event Fired!"); });