javascript - Typescript & Jquery: what is the best practice to call a class into Jquery onclick function? -
i don't want instantiate class in each function. how to? should best practice organize 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(); });
the best workaround (i think) can restructure class return functions desires. example of mean:
var myclass = function(){ return { set: function(a, b, c){ return [a, b, c].tostring(); }, modify: function(a){ return + ' .)'; } } }; $(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, way instantiate class once. check jsfiddle
Comments
Post a Comment