Skip to content
jbaicoianu edited this page Oct 27, 2012 · 4 revisions

Elation has several facilities to make dealing with browser events simple:

  • elation.events.add(domElement, eventTypes, handlerFunc);
  • elation.events.remove(domElement, eventTypes, handlerFunc);
  • elation.events.fire({element: eventSource, type: eventType, data: ...});

Here is an example of a simple component which listens for click events, performs a task, and then fires its own event when it's done.

    // Example Javascript component
    elation.component.add('test.foo', function() {
      this.numclicks = 0; // simple member variable
      
      this.init = function() {
        // Register event handlers for the DOM element associated with this component
        elation.events.add(this.container, "click", this);
      }
      this.doSomething = function() {
        // Perform some logic, then fire an event to let any listeners know I'm done
        this.numclicks++;
        elation.events.fire({element: this, type: 'fooDidSomething', data: this.numclicks});
      }
      this.click = function(ev) {
        // Handle click events
        this.doSomething();
      }
    }

Bridging Events with Native Apps

If you are developing a native app which loads your website in a UIWebView/android.webkit.WebView/etc, you can bridge events between the webapp and your native app with a small amount of code. Your app tells the webapp which events it wishes to be notified of by injecting some JS code into the WebView:

elation.native.subscribe('eventname1,eventname2');

Whenever a matching event is fired within the UIWebView, it's passed on to the native client. With webkit-based browsers, this is done by loading a URL of the format elation-event:<eventType>/<eventData> which can be captured and handled with a WebView delegate.

iOS

An Objective-C library is provided to make working with bridged Elation events easy. After binding the bridge to a UIWebView and subscribing to the needed events, these events will be passed through the NSNotificationCenter and can be handled anywhere in your app you want to listen for them.

// Objective-C (iOS)
// First init bridge, bind to specified UIWebView, and subscribe to specified events
[[ElationEventBridge defaultBridge] attachWebView:myWebView subscribe:@"user_login,user_logout"]

// Then any class can add notification observers to watch for the "elation-event" notification
@implementation MyClass 
- (id) init {
  [[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(handleElationEvent:) 
      name:@"elation-event"];
}
- (void) handleElationEvent:(NSNotification *) notification {
  NSDictionary *event = notification.userInfo;
  if ([event.type isEqualToString:@"user_login"]) {
    NSDictionary *user = event.data;
    ...
  }
}
@end

Android

(TODO - implement event bridge which exposes web events via the Intent system)

Metro

Metro C# apps expose this functionality via window.external.notify(). The subscription process is the same as mentioned above, and then your app can handle ScriptNotify events. See MSDN's documentation for details.

Clone this wiki locally