|
|
|
|
EventsFrom $1Table of contentsGigya Socialize generates many different events for various situations which are driven by user interactions. For example, when a user logs in. A program may listen to events which are relevant to it. A program interested in certain events may register event handlers for those events and execute code when those events are received.
This section elaborates the list of events that Gigya Socialize generates, and specifies how to: define events handlers and register for events.
The list of events generated by Gigya Socialize is divided into two types of events:
Defining an Event Handler - is performed in the same way for both types of events Registering a Handler for an Event - is performed differently for each type of events.
Global Application EventsGlobal application events are generated by Gigya Socialize whenever the event they refer to occurs, regardless of what was the action that triggered the event. This is in contrast to Widget events, which are only fired by the specific Widget they were configured on.
The following is a list of available Global Application Events:
Defining an Event HandlerAn Event Handler is a function with the following signature: The eventObj includes the following members:
Example: function DisplayEventMessage(eventObj) {
alert(eventObj.context.str+' '+eventObj.eventName);
}
function DisplayEventMessage(eventObj:Object): void {
trace(eventObj.context.str+' '+eventObj.eventName);
}
Registering a Handler for an EventRegistering a Handler for an event is done using the Socialize API method - socialize.addEventHandlers Like any other Gigya API method, addEventHandlers receives two parameters: conf object and params object (Please refer to the Socialize Basics page for more information). The params object may include the following members (all optional):
Example: gigya.services.socialize.addEventHandlers(conf, {
context:{str:'congrats on your '},
onLogin:DisplayEventMessage
}
);
In this example we register the "user login" event to the 'DisplayEventMessage' function. This means - that whenever the user logs in, the DisplayEventMessage function will be called, and it will receive the eventObj parameter enfolding - the context object (passed to addEventHandlers) and the eventName (which, in this case is 'login'). The output in this example would be:
Notes:
Example: function displayEventMessage(eventObj) {
alert(eventObj.context.str+' '+eventObj.eventName);
}
function confirmEventMessage(eventObj) {
var r = confirm('Have you '+eventObj.eventName+ '?');
if (r==true)
alert(eventObj.context.str);
}
gigya.services.socialize.addEventHandlers(conf, {
context:{str:'congrats on your'},
onLogin:displayEventMessage,
onConnect:confirmEventMessage
}
);
gigya.services.socialize.addEventHandlers(conf, {
context:{str:'sorry to see you'},
onDisconnect:displayEventMessage
}
);
function displayEventMessage(eventObj:Object): void {
trace(eventObj.context.str+' '+eventObj.eventName);
}
function addEventHandlers():void {
gigya.services.socialize.addEventHandlers(conf, {
context:{str:'congrats on your'},
onLogin:displayEventMessage,
onConnect:displayEventMessage
}
);
gigya.services.socialize.addEventHandlers(conf, {
context:{str:'sorry to see you'},
onDisconnect:displayEventMessage
}
);
}
Working ExamplePlease explore the following pages, where you may activate full working examples and then grab the code:
Widget EventsEach Widget defines events which it generates for specified situations. The following events are generated by all Widgets:
In addition to the above events, the Friend Selector Widget generates the following event:
Note: The onSelectionDone event is fired only if friends were selected. If the user did not select any friends and pressed the "OK" button, an onClose event is fired instead.
Defining an Event HandlerSimilarly to Defining an Event Handler for "Global Application Events," an Event Handler is a function with the following signature:
The eventObj object includes members, which correspond to the specific event that was fired.
For example - for onSelectionDone event, the eventObj will include the following members:
Example of Usage: function onSelectionDoneHandler(eventObj:Object): void {
var msg:String = eventObj.eventName;
var friends:Object = eventObj.friends;
if ( null!=friends) {
var friendsArr:Array = friends.asArray();
if ( null!=friendsArr && friendsArr.length>0) {
for (var index:Object in friendsArr ) {
var friend:Object = friendsArr[index];
var name:String = ' - Friend\'s Name is :'+ friend['nickname'];
msg += name + '\n';
}
}
else {
msg += ' - Error: Friends list is empty or null';
}
}
else {
msg += ' - Error: No friends were returned ';
}
trace(msg);
};
function onSelectionDoneHandler(eventObj:Object): void {
var msg:String = eventObj.eventName;
var friends:Object = eventObj.friends;
if ( null!=friends) {
var friendsArr = friends.asArray();
if ( null!=friendsArr && friendsArr.length>0) {
for (var index in friendsArr ) {
var friend = friendsArr[index];
var name = ' - Friend\'s Name is :'+ friend['nickname'];
msg += name + '\n';
}
}
else {
msg += ' - Error: Friends list is empty or null';
}
}
else {
msg += ' - Error: No friends were returned ';
}
trace(msg);
};
The following members of the eventObj are available for all events:
Registering a Handler for an EventThe registration for Widgets events is performed during the creation of the Widget. Each one of the Widgets' creation methods (such as - Socialize.showLoginUI) receives, as part of the 'params' objects, the following parameters (all optional):
In addition the socialize.showFriendSelectorUI method receives, as part of the 'params' object, the following parameter:
Each of the above parameters may be assigned a value, which is the Event Handler function name that will be called whenever the corresponding event occurs. Example: function onLoadHandler(eventObj) {
alert(eventObj.context.str + " loaded");
}
function onSelectionHandler(eventObj) {
alert("User selected friends and pressed OK on " + eventObj.context.str);
}
gigya.services.socialize.showFriendSelectorUI(conf,
{
context:{str:'Friends Selection dialog'},
onLoad:onLoadHandler,
onSelectionDone: onSelectionHandler
}
);
function onLoadHandler(eventObj:Object): void {
trace(eventObj.context.str + " loaded");
}
function onSelectionHandler(eventObj:Object): void {
trace("User selected friends and pressed OK on " + eventObj.context.str);
}
function showFriendSelectorUI(): void {
gigya.services.socialize.showFriendSelectorUI(conf, {
context:{str:'Friends Selection dialog'},
onLoad:onLoadHandler,
onSelectionDone: onSelectionHandler
});
}
Working ExamplePlease explore "Send notifications" Example where you may activate a full working example and then grab the code. The example uses socialize.showFriendSelectorUI and onSelectionDone event, as well as sending a notification to friends.
< Back to 'Using Socialize Widgets' | Next to 'Authentication' >
Tags:
|
||||||||||||
|