views:

109

answers:

4

On mootools I'm used to declare once and reuse often. An FX example would go like this:

myEffect1 = new Fx.Slide($('myElement1'));

How should I go on jQuery? Meaning, the docs make it straightfoward to use:

$('myElement1').click(function(e){
    this.slideToggle();
});

But if I want to call this effect somewhere else on my code will I have to re-declare it? And isn't this approach more resource hungry than the one above? How would this be properly done on jQuery?

+1  A: 
var slideToggleEffect = function(e){
    this.slideToggle();
};

$('myElement1').click(slideToggleEffect);
$('myElement2').click(slideToggleEffect);
...
HappyCoder
Thanks for pointing this out! Nevertheless you code does not create a reusable instance of the effect. It reuses a function - which is great - but still requires SIZZLE to go over all the elements every time you use the click event if you are animating other element than the one clicked.
Frankie
JQuery is centered around object that is created via jQuery(...) or $(...).Try playing with jQuery.fn.slideToggle.
HappyCoder
Thanks HappyCoder! You comment and dcneiner answer made perfect sense now. Its just that mootools returns an element on $() and jQuery returns an object. Have to get used to that.
Frankie
jQuery.fn.slideToggle.apply(jQueriedElement);But it still requires to call jQuery(...) on DOM element anyway :)
HappyCoder
+3  A: 

Just manually cache the result set in a variable before calling the function, then reuse the function as needed:

var $el_one = $("#path .to > .selection"), // Stores jQuery object
    $el_two = $("#path .to > .second");    // Stores jQuery object

var effect = function(){
    $el_one.fadeIn();
    $el_two.fadeOut();
}

Now you can call effect any time without reselecting the items. They instead use the cached jQuery selection to animate correctly.

If you need more clarity, let me know.

Doug Neiner
This is perfect. As jQuery $() returns a jQuery object and not the element it will work just like on Mootools. Thanks!
Frankie
+1  A: 

I'd go with a plugin in this case. For example here's a plugin that I use often - a very simple slide and fade toggle effect.

$.fn.slideFadeToggle = function(easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, "fast", easing, callback);
};

Now you can call slideFadeToggle on any selector like this:

$("#somedom").slideFadeToggle();
Andy Gaskell
Even though it is not what I was looking this a great input Andy, thanks!
Frankie
Yeah, if you often use the same function across the application it will be very efficient to store it in $.fn (which is the same as $.prototype). Every newly created jQuery('#element') will inherit everything from $.fn.
HappyCoder
+1  A: 

Since every command acts on a jQuery object (the object returned by calling $() on a selector), the example you've given is the nearest comparison to what you're used to in MooTools, as far as I can see.

Is it more resource hungry? Well, that's a complex question to answer as instantiating objects is only one piece of client side code. Some framework methods for performing certain operations are better in some situations and worse in others.

Russ Cam
Thanks Russ! Actually I was concerned on having to put Sizzle to work every time I called the XYZ effect because it's triggered by several different elements. To store the $('animated_element') result and then reuse it as dcneiner pointed out makes it more Moo-like.
Frankie
@Frankie - absolutely cache a jQuery object in a local variable if it will be used more than once. I think that's the third rule of jQuery club (oh dear, I've broken rules one and two) :)
Russ Cam