Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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?

— asked August 11th 2010 by Carl Jahn
  • event
  • listener
  • module
0 Comments

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;
    
    — answered February 14th 2011 by Robert R
    permalink
    0 Comments
  • push

    — answered August 16th 2010 by Carl Jahn
    permalink
    0 Comments
  • any idea How to do this on android ?

    — answered October 14th 2012 by Visuddha Karunaratne
    permalink
    1 Comment
    • this looks promising, thank you

      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
      
      }
      

      — commented October 14th 2012 by Visuddha Karunaratne
  • 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
    
    }
    
    — answered October 14th 2012 by Ashish Nigam
    permalink
    1 Comment
    • How do you then listen for than event in Javascript?

      — commented July 31st 2013 by Peter Kamb
  • 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!");
    });
    
    — answered July 31st 2013 by Peter Kamb
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.