Typescript & jquery: what is the best name for a class in a jquery onclick function?

I do not want to instantiate a class in every function. How? What should be the best practice for organizing this in Typescript syntax?

$(".container_dettaglio .varianti .variante a").click(function (event) { event.preventDefault(); var pr = new Prodotto($(this).data('variante')); pr.cambiaVariante(); }); $(".meno").on('click',function(e){ e.preventDefault(); var pr = new Prodotto($(this).data('variante')); pr.rimuoviQuantita(); }); $(".piu").on('click',function(e){ e.preventDefault(); var pr = new Prodotto($(this).data('variante')); pr.aggiungiQuantita(); }); 
+5
source share
1 answer

The best workaround (I think) you can do is restructure your class to return functions that will make your wishes. This is an example of what I mean:

 var MyClass = function(){ return { Set: function(a, b, c){ return [a, b, c].toString(); }, Modify: function(a){ return a + ' .)'; } } }; $(function(){ var mc = new MyClass(); $('.a').on('click', function(){ alert( mc.Modify($(this).data('variant')) ); }); $('.b').on('click', function(){ alert( mc.Modify($(this).data('variant')) ); }); }); 

So this way you instantiate your class once. Check jsFiddle

+2
source

All Articles