Is it bad form to simulate a click to save repeating myself?


#1

In my application I have some simple logic behind a dialog window which pops up to show a login form. There is a [x] button to close the dialogue.

I have some events mapped to action the buttons on the form including that [x] to close the dialog.

Rather than repeat myself on a successful login, I can drop in a $('.close_button').click() which will then take advantage of the events I already have.

The only other option I have would be to write a function in the global namespace to handle closing of the dialog, and call that in the helper and on a successful login.


#2

IMO yes it is very bad unless you need to simulate a click for a different reason.


#3

Yeah I didn’t think it was a good way to go, kind of why I asked.

Thanks :smile:


#4

IMO, this can lead to undesired behaviours if you have multiple event listeners on the element in pure JS. You can name events with jQuery and probably the same with Meteor’s event maps. That way you are probably good to go.


#5

As you suggested, I think it would be best to abstract the logic/code for closing the modal into its own function. Then on the [X] click handler or the end of your login flow you can call the same closeModal() function to visually hide it.


#6

Thanks, yeah that is what I have now done.
I just didn’t like the idea of it because now I have that in the global namespace, when really it would have been nicer to attach a function to the template which the helpers and events drive.