Skip to content

Instantly share code, notes, and snippets.

@alexed1
Created October 20, 2017 00:36
Show Gist options
  • Save alexed1/def661ac934c2be3fe2e1c28952407c4 to your computer and use it in GitHub Desktop.
Save alexed1/def661ac934c2be3fe2e1c28952407c4 to your computer and use it in GitHub Desktop.
main component helper
({
publishRecommendationRequest : function(component, event, helper) {
//Add callback behavior
//
console.log("inside publishRecommendationRequest");
this.callApex('c.pubRecommendationRequest','successfully sent Recommendation Request',component);
},
//centralize the mechanism for calling Apex
callApex : function(methodName, successMethod, component){
console.log("inside callApex");
var method = methodName;
var action = component.get(method);
console.log("action is:" + action);
action.setCallback(this, function(response) {
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
console.log(successMethod);
}
else {
console.log("Failed with state: " + state);
}
});
$A.enqueueAction(action);
},
connectCometd : function(component) {
var helper = this;
// Configure CometD
var cometdUrl = window.location.protocol+'//'+window.location.hostname+'/cometd/40.0/';
var cometd = component.get('v.cometd');
cometd.configure({
url: cometdUrl,
requestHeaders: { Authorization: 'OAuth '+ component.get('v.sessionId')},
appendMessageTypeToURL : false
});
cometd.websocketEnabled = false;
// Establish CometD connection
console.log('Connecting to CometD: '+ cometdUrl);
cometd.handshake(function(handshakeReply) {
if (handshakeReply.successful) {
console.log('Connected to CometD.');
// Subscribe to platform event
var newSubscription = cometd.subscribe('/event/Recommendation_Generated__e',
function(platformEvent) {
console.log('Platform event received: '+ JSON.stringify(platformEvent));
helper.onReceiveEvent(component, platformEvent);
}
);
// Save subscription for later
var subscriptions = component.get('v.cometdSubscriptions');
subscriptions.push(newSubscription);
component.set('v.cometdSubscriptions', subscriptions);
}
else
console.error('Failed to connected to CometD.');
});
},
disconnectCometd : function(component) {
var cometd = component.get('v.cometd');
// Unsuscribe all CometD subscriptions
cometd.batch(function() {
var subscriptions = component.get('v.cometdSubscriptions');
subscriptions.forEach(function (subscription) {
cometd.unsubscribe(subscription);
});
});
component.set('v.cometdSubscriptions', []);
// Disconnect CometD
cometd.disconnect();
console.log('CometD disconnected.');
},
onReceiveEvent : function(component, platformEvent) {
var helper = this;
console.log("received an event...");
console.log(platformEvent.Name);
// Extract data from platform event
//var newNotification = {
// time : $A.localizationService.formatDateTime(
// platformEvent.data.payload.CreatedDate, 'HH:mm'),
// message : platformEvent.data.payload.Message__c
// };
// Save notification in history
//var notifications = component.get('v.notifications');
//notifications.push(newNotification);
component.set('v.label', platformEvent.data.payload.PropositionLabel__c);
component.set('v.description', platformEvent.data.payload.PropositionDescription__c);
component.set('v.id', platformEvent.data.payload.PropositionId__c);
component.set('v.followupFlowId', platformEvent.data.payload.PropositionFollowupFlowId__c);
component.set('v.acceptLabel', platformEvent.data.payload.PropositionAcceptLabel__c);
},
displayToast : function(component, type, message) {
var toastEvent = $A.get('e.force:showToast');
toastEvent.setParams({
type: type,
message: message
});
toastEvent.fire();
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment