Event declaration
on registers an event; in this case the locationloaded. And since the locationloaded is a custom event and the only way to fire the event is through the trigger method.
Registers an Event customevent and attach the function locationloaded
$(document).on('customevent', locationloaded);
function locationloaded() {
console.log('The custom event is triggered inside the "locationlaoded" function');
}
Fires the Event: customevent
$(document).trigger("customevent");
In turn, logs the message The custom event is triggered inside the "locationlaoded" function
Using trigger vs calling the function directly; i.e. locationloaded()
Using trigger broadcast a message to the DOM that an event is fired. So any separate javascript file that listens to an event immediately notified
$(document).trigger('subscribe');
On the same file as $(document).trigger('subscribe')
$(document).on('subscribe', function() {
// Send email
});
On the other javascript file
$(document).on('subscribe', function() {
// Save data
});
Whereas calling a declared function directly only allows any other performing tasks within the same context of the method called.
That's why the $(document).trigger('locationloaded)` is declared in a separate file, at the same time if the event is triggered and broadcast, then, the other file can perform its tasks.