You are not logged in.   Log in | Register

Events

From $1

Gigya 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:

  • Global Application Events - Events which are initiated by the application (not by a Widget). For example, when a user logs in.
  • Widget Events - Events which are initiated by a Widget. For example, when a user clicks on the "OK" button of a Widget.

 

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 Events 

Global 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:

  • onLogin
  • onLogout
  • onConnect
  • onDisconnect

 

Defining an Event Handler

An Event Handler is a function with the following signature:

functionName(eventObj)

functionName(eventObj:Object): void

The eventObj includes the following members:

  • eventName property - the name of the event fired. May be: 'login', 'connect' or 'disconnect' respectively.
  • context property - will be available only if passed as a parameter while registering for the specific event (This point will be further elaborated in the following section - "Registering a Handler for an Event").

 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 Event

Registering 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):

  • onLogin - The value assigned to this parameter is a name of an Event Handler function that will be called when the user is successfully authenticated by an external provider?.  
  • onLogout - The value assigned to this parameter is a name of an Event Handler function that will be called when the user logs out.
  • onConnect - The value assigned to this parameter is a name of an Event Handler function that will be called when the user is successfully connected to a provider.
  • onDisconnect - The value assigned to this parameter is a name of an Event Handler function that will be called when the user disconnects from a provider.
  • callback - A reference to a callback function that will be called, along with the results of the operation when the operation completes.
  • context - A developer created object that will be passed back to the application as part of the eventObject, which is passed to the Event Handler function.

 

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:

Alert.jpg

 

Notes:

  • Several events may be registered to one Event Handler (function)
  • Several events may be registered within one addEventHandlers method call 

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 Example

Please explore the following pages, where you may activate full working examples and then grab the code:

Widget Events

Each Widget defines events which it generates for specified situations.

The following events are generated by all Widgets:

  • onLoad - When the Widget has finished drawing itself.
  • onError - If an error occurs.
  • onClose - When the Widget is closed. Widgets may be "closed" in either of the following ways:
    • If the Widget is opened as a popup dialog, an "X" button would appear. Clicking the "X" button will close the Widget and an onClose event is generated.
    • In case the Widget has an "OK" and/or "Cancel" button, clicking either buttons will close the Widget and an onClose event is generated.

 

In addition to the above events, the Friend Selector Widget generates the following event:

  • onSelectionDone - When the user selects friends and presses the "OK" button.

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 Handler

Similarly to Defining an Event Handler for "Global Application Events," an Event Handler is a function with the following signature:

 

functionName(eventObj)

functionName(eventObj:Object): void

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:

Field Type Description
eventName string The name of the event.
context object The context object passed as a parameter to the method, or null if no object has been passed.
friends Collection A collection of basic? Friend objects, representing the list of the selected friends.

 

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:

  • eventName property - the name of the event fired.
  • context property - will be available only if passed as a parameter while registering for the specific event. (This point will be further elaborated in the following section).

Registering a Handler for an Event

The 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):

  • onLoad
  • onError
  • onClose

 

In addition the socialize.showFriendSelectorUI method receives, as part of the 'params' object, the following parameter:

  • onSelectionDone

 

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 Example

Please 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:
Files (0)