0

I just saw two javascript files:

first one had called following line after some success ajax call:

$(document).trigger("locationloaded");

The second js had:

$(document).on('locationloaded', function () {
   //few lines of code here
});

Bit new to JS, just curious to know, could there be any valid reason for calling locationloaded through trigger? Why can't we have simple function as locationloaded() which would be called from first js? If both do the same work, which one should be used?

4
  • api.jquery.com/trigger Commented Jul 5, 2017 at 19:09
  • 1
    Elements are propagated, and jquery is based on the dom. Thats the usecase for .trigger... Commented Jul 5, 2017 at 19:13
  • So, we should use trigger only for dom? not for scenario like above? Commented Jul 5, 2017 at 19:17
  • This is the problem when you work with anonymous functions. Since you define the method for the event without a name, you'll need to trigger the event instead of directly calling the method. Commented Jul 5, 2017 at 20:03

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Didn't answer my question, why can't I use simple function call in above scenario?
I think you're confused by the naming of your posted code. I updated my code to reflect the new approach. Clearly locationloaded parameter in your trigger function is the same event name declared on the on function. And since it is a custom event the only way to trigger is by explicitly using the trigger function.
Okay. What is the difference if I call $(document).trigger("customevent"); VS i call locationloaded() directly?
0

As per documentation of jQuery trigger

Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method.

1 Comment

Didn't answer my question, why can't I use simple function call in above scenario?
0

This is due to following a software engineering principle of having "high cohesion and low coupling" for higher quality software.

By using events you can define zero to many handlers that can do different things for different purposes. Don't need to do anything when locationloaded? Great! Have three separate things that need to be done for three different purposes defined in different places? No problem! Each event handler only has to be concerned with its own job. This is a very flexible arrangement - high cohesion.

If you just called a function, one and only one function defined in one place must exist. This function must serve every purpose no matter if those purposes are not related. The function must exist whether needed or not. Very rigid higher coupling, lower cohesion arrangement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.