0

I currently have some code written that will call a function after a custom .trigger() call. This is what is called:

function _visitAddTag(state, listener) {
        if (state.properties.action = "RedoAddTag") contextMenu.unhide();
        var $target = contextMenu.getContextMenuUI().tags; // Grab tag elements
        var callback = function () {
            if (listener) {
                google.maps.event.removeListener(listener);
            }
            $target.off("tagIds-updated", callback);
            contextMenu.hide();
            next.call(contextMenu.getTargetLabel(), state.transition);
        };
        $target.on("tagIds-updated", callback());
    }

The next.call() line causes this method to be triggered:

function () { // 'this' is contextMenu.getTargetLabel(), as called in Onboarding.js/_visitAddTag()
                tracker.push('Onboarding_Transition', {onboardingTransition: "tag-attribute-1"});
                var tags = this.getProperty('tagIds');
                return tags == [2] ? "adjust-heading-angle-1" : "redo-tag-attribute-1" // Where 2 is the tag_id of the "points into traffic" tag
            }

Where a return value of "redo-tag-attribute-1" will cause the whole thing to loop again.

I see the potential for recursion here, especially if the "tagIds-updated" event is somehow triggered inside of the second function call. However, when I debug the code, that event is only triggered once. Does anyone have any ideas as to what is happening here? I'm a javascript newb so maybe I'm missing something obvious.

Lukas Strobel
  • 108
  • 1
  • 5
  • `.on("tagIds-updated", callback())` you should remove the `()` beside `callback`. It will run the callback immediately instead of waiting for the event. – Keno Feb 25 '19 at 03:06

1 Answers1

1

I can't wrap my head around the code completely. I see 2 problems in your code at the moment:

  1. $target.on("tagIds-updated", callback()); doesn't really attach an event handler. It's just calling callback immediately. You need $target.on("tagIds-updated", callback);. Notice the missing ().
  2. tags == [2] will always be false. Why?. You can probably use tags.includes(2) instead. As it stands, that function will always return "redo-tag-attribute-1". Which maybe the root cause of this recursion that you're facing.
maazadeeb
  • 5,922
  • 2
  • 27
  • 40
  • Oh my goodness, it really was just the extra () after the `callback` call. I didn't realize that that would just call it right away. Thank you so much – Lukas Strobel Feb 25 '19 at 03:19