Appcelerator Titanium iOS module access event store

136 views Asked by At

I am writing an iOS module to implement non existing functionality in the calendar. Can I access the app's event store in an iOS module rather than creating my own ?

1

There are 1 answers

1
Dawson Toth On

The typical pattern is to add event listeners to some proxy returned by your module. When some event happens that you want to emit from your module to be handled by the JavaScript code, you can do so.

Your JS can register your callbacks by calling a method on your module:

-(void)registerCallbacks:(id)args
{
    ENSURE_SINGLE_ARG(args, NSDictionary);

    NSLog(@"[KROLLDEMO] registerCallbacks called");

    // Save the callback functions and retain them
    successCallback = [[args objectForKey:@"success"] retain];
    cancelCallback = [[args objectForKey:@"cancel"] retain];
    requestDataCallback = [[args objectForKey:@"requestData"] retain];

    NSLog(@"[KROLLDEMO] Callbacks registered");
}

... which is called by your JS:

var yourModule = require('your.module');
yourModule.registerCallbacks({ success: ..., etc })

... and when your module wants to let your JS know what's going down:

if (successCallback != nil){    
    NSMutableDictionary *event = [NSMutableDictionary dictionary];
    [event setObject:@"foo" forKey:@"message"];
    [event setObject:@"bar" forKey:@"title"];
    [self _fireEventToListener:@"success" withObject:event listener:successCallback thisObject:nil];
}

See more examples here: https://github.com/appcelerator-modules/ti.moddevguide/search?utf8=%E2%9C%93&q=fire