Javascript Event System listen() Example
From KickApps Documentation
Contents |
Basic event listener
This example shows the simplest form of event listening. When the "play-page-load" event is dispatched, the JavaScript function onPlayPageLoad will be executed with no parameters.
function onPlayPageLoad() {
// do something here! :)
}
var newEventId = Ka.events.listen('play-page-load', onPlayPageLoad);
Listen for an event and handle its data
This example shows how to listen for an event and handle the data that gets passed from the event the event handler you have defined. The variable data is used in this example, but you may name the parameter anything you like. The properties of the data parameter are defined by the event itself. Please refer to the documentation for each event to learn what properties are exposed.
function onCommentDeleted(data) {
// do something here! :) alert(data.commentId);
}
var newEventId = Ka.events.listen('profile-page-delete-comment-success', onCommentDeleted);
Target a specific browser
This example shows how to target a specific browser when handing the login popup by specifying an optional third parameter.
function onLoginPopupDisplayIE(data) {
// do something here! :)
// This will only happen in IE
console.log("The HTML element that caused this popup to display is: " + data.triggeredBy);
}
var newEventId = Ka.events.listen('global-login-popup-display', onLoginPopupDisplayIE, {browser: "MSIE"});
Changing the scope of the event handler
This example shows how you can specify the scope of the 'this' variable in your event handler.
The first example illustrates that 'this' will refer to the window object if you do not specify scope:
function testWithoutScope(data) {
console.log(this); // prints out the window object
} var newEventId = Ka.events.listen('global-login-popup-display', testWithoutScope);
The second example illustrates that 'this' will refer to the first form on the document:
function testWithScope(data) {
console.log(this); // prints out the first form on the document
} var newEventId = Ka.events.listen('global-login-popup-display', testWithScope, {scope: document.forms[0]});
Favorites









