0

I have some events, and to make them a bit neater I have grouped them together in a module

export function myevents () {


$('.btn').on('click', function() {
   //do stuff
  });


 $(document).on('click', ".delete-product", function(){

  //Do stuff

 });

}

There are loads of them but they are only really related by the fact they are dom events. The events mainly update the DOM and perform ajax request.

The problem is it is just a bit messy. What is the best way to tackle this. Should I add them to a class?

I am using Jquery and am stuck with it for now.

LeBlaireau
  • 17,133
  • 33
  • 112
  • 192
  • 2
    This is very opinion based on what people consider messy or not. – Deckerz Sep 05 '17 at 09:42
  • 2
    If you want to use ES6, you should use `() => {` instead of `function() {` :) – Jeremy Thille Sep 05 '17 at 09:42
  • 1
    @JeremyThille Depends whether you want `this` to be the element or not. It's not a straightforward replacement. – James Thorpe Sep 05 '17 at 09:47
  • Yes that's true, depends on the context you want inside. – Jeremy Thille Sep 05 '17 at 09:48
  • 1
    @JeremyThille—you mean it depends on the value of *this* that you want inside. For listeners, typically it should be a reference to the element. – RobG Sep 05 '17 at 09:50
  • 1
    I think it doesn't make much sense to group functions because they share common technical characteristics. You should rather arrange them according to their purpose in the respective domain model. If you put them in a class as static properties, you just create a namespace, resulting in more keystrokes. You see, it's mainly opinion based. –  Sep 05 '17 at 10:28

0 Answers0