Skip to content

Instantly share code, notes, and snippets.

@shrutis22
Last active November 29, 2017 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrutis22/a3afc52c5fd052d7684591905df6d34d to your computer and use it in GitHub Desktop.
Save shrutis22/a3afc52c5fd052d7684591905df6d34d to your computer and use it in GitHub Desktop.
/**
* This JavaScript file is created
* to initiate the CometD library,
* register CometD listeners, perform
* Channel Subscription and various
* other actions.
*
* @author Shruti Sridharan
* @since 27.11.17
* @revisions N/A
**/
var AnnouncementsApp = {
constants : {},
actions : {
/**
* Intialize the CometD library
* with the URL and the Session
* ID for authentication.
**/
initCometD : function() {
$.cometd.init(
{
url : AnnouncementsApp.constants.COMETD_URL,
requestHeaders : {
Authorization : "Bearer " + AnnouncementsApp.constants.SESSION_ID
}
}
);
},
/**
* Register to all the CometD Events
* such as Handshake, Connect and
* Disconnect.
*/
registerCometDListeners : function() {
/**
* Do the Subscription only after
* the "Handshake" was successful.
*/
$.cometd.addListener(
"/meta/handshake",
function( message ) {
if( message.successful ) {
console.info( new Date() + " : CometD Handshake Completed." );
AnnouncementsApp.actions.subscribeToChannel();
}
else {
/**
* If the Handshake failed we
* need to restart the init
* process.
*/
console.info( new Date() + " : CometD Handshake Failed." );
AnnouncementsApp.actions.initCometD();
}
}
);
/**
* If it's getting disconnected
* we need to restart the init
* process.
*/
$.cometd.addListener(
"/meta/connect",
function( message ) {
if( $.cometd.isDisconnected() ) {
console.info( new Date() + " : CometD Disconnected." );
AnnouncementsApp.actions.initCometD();
}
}
);
/**
* Restart the init process if
* the CometD has disconnected.
*/
$.cometd.addListener(
"/meta/disconnect",
function( message ) {
console.info( new Date() + " : CometD Disconnected." );
AnnouncementsApp.actions.initCometD();
}
);
},
/**
* Subscribe to the Push Topic
* aka the Channel.
**/
subscribeToChannel : function() {
//Compile the Handlebars Template
var source = $( "[id='announcement-template']" ).html();
var template = Handlebars.compile( source );
$.cometd.subscribe(
AnnouncementsApp.constants.CHANNEL_URL,
function( message ) {
$( "#divNoAnnouncements" ).hide();
//Fill the Handlebars Merge Fields
var html = template( message.data.sobject );
$( "#divAnnouncements" ).append( html );
//Display a Desktop Notification
Push.create(
message.data.sobject.Name,
{
body: message.data.sobject.Details__c,
icon: AnnouncementsApp.constants.ICON_PATH,
timeout: 4000,
onClick: function () {
window.focus();
this.close();
}
}
);
}
);
},
/**
* Mark an Announcement as read
* by removing it from the VF
* Page as well as creating a
* record in the Acknowledged
* Users object to mark the
* acknowledgment.
**/
dismissAnnouncement : function( annId ) {
//Using VF Remote Objects to perform the CRUD
var ack = new SObjectModel.Acknowledged_User__c( { Announcement__c : annId } );
ack.create(
function( err ) {
if ( err ) {
alert( "Failed to Acknowledge! Error: " + err );
}
else {
$( "#" + annId ).fadeOut();
}
}
);
}
},
eventHandlers : {
dismissAnnouncement : function( annId ) {
AnnouncementsApp.actions.dismissAnnouncement( annId );
}
},
init : function( config ) {
AnnouncementsApp.constants = config;
AnnouncementsApp.actions.registerCometDListeners();
AnnouncementsApp.actions.initCometD();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment