ugrás a tartalomhoz

jQuery: a this az eseménykezelő metódust tartalmazó osztály legyen

Hodicska Gergely · 2008. Május. 12. (H), 19.24
Sziasztok!


Ezt tényleg csak így lehet jQuery esetében?
  1. $(document).ready(function () {  
  2.     monitoring = new Monitoring();  
  3. });  
  4.   
  5. var Monitoring = function()  
  6. {  
  7.     var thisClassHack = this;  
  8.   
  9.     this.isRunning = false;  
  10.   
  11.     $('#toggleMonitoring').click(function(event) {  
  12.         return thisClassHack.toggleMonitoring.apply(thisClassHack, [event]);  
  13.     });  
  14. }  
  15.   
  16. Monitoring.prototype = {  
  17.     toggleMonitoring: function(event) {  
  18.         alert(event);  
  19.         alert(this.isRunning);  
  20.     }  
  21. }  
Üdv,
Felhő
 
1

kis szépségtapasz

Hodicska Gergely · 2008. Május. 12. (H), 19.52
  1. (  
  2.     function ($) {  
  3.         $.delegate = function (scope, func) {  
  4.             var _scope = scope;  
  5.   
  6.             return function () {  
  7.                 func.apply(_scope, arguments);  
  8.             }  
  9.         }  
  10.     }  
  11. )(jQuery);  
  12.   
  13.   
  14. $(document).ready(function () {  
  15.     monitoring = new Monitoring();  
  16. });  
  17.   
  18. var Monitoring = function()  
  19. {  
  20.     this.isRunning = false;  
  21.   
  22.     $('#toggleMonitoring').click($.delegate(thisthis.toggleMonitoring));  
  23. }  
  24.   
  25. Monitoring.prototype = {  
  26.     toggleMonitoring: function(event) {  
  27.         alert(event.target);  
  28.         alert(this.isRunning);  
  29.     }  
  30. }