views:

55

answers:

2

In order to clean up my code i want to use sub-plugins within my actual jQuery plugin, but actually there is nothing happening. thx in advance

As an easy example, please take a look at the following code:

(function($){
    $.fn.funct = function() {
     // so far it seems to run the code...
     console.log('funct is running...');

     return this.each(function(){
      // ...but nothing is happening here
      console.log('this.each is running...');
      $(this).css('background', 'blue');
     }
    } 
    $.fn.foo = function() { 
     return this.each(function(){
      console.log('plugin is running...');
      $(this).funct();
     });
    };
})(jQuery);
A: 

I would prefer to fire a custom event in one plugin and let the other plugin subscribe to that event. The you do not have the dependency.

See my answer here for more info regarding custom events and binding/triggering

redsquare
thx for the tip
Julian Weimer
+1  A: 

At initial glance, it looks like you're not closing the first return properly.

$(this).css('background', 'blue');
        }

should be:

$(this).css('background', 'blue');
        });
Sean O
Wow, good catch!
Patrick McElhaney