malsup / cycle2

2nd gen cycling
899 stars 236 forks source link

pre-initialize event sometimes fires before center plugin pre-initialize listener is set #303

Open albell opened 11 years ago

albell commented 11 years ago

There are variations on the same problem that I see all over the tracker here, where people are attaching a listener for something that's sometimes already fired, and wondering why it isn't working. I'm running into it myself, in a situation where the pre-initialize and/or initialized events are sometimes firing before (for example) the center plugin's listeners are set. Because it's an intermittent problem, it's hard for novices to isolate and describe. It just looks broken. Options I thought of:

1) Put center in the main distribution, and move the default init code out of the core engine, to the end of the file:

// automatically find and run slideshows
$(document).ready(function() {
    $( $.fn.cycle.defaults.autoSelector ).cycle();
   });

Yuck, possibly unnecessary weight.

2) Leave things as is, but tell authors using center to call the cycle imperatively using a non-default selector, after any plugins are loaded. If that's truly required maybe there should be a warning about it in the docs. Took me a good loooong while to figure this out :)

3) Refactor center to work on slideshows whose pre-initialized event has already fired, while still firing on future initializations. A crude way might be to just add a single check to see if the document has any elements with the default selector, and then trigger the pre-initialize event again, as a precaution.

Maybe you have a better idea for a generalized solution, that maintains the nice modular architecture and killer event system?

malsup commented 11 years ago

Are you loading the cycle scripts after the domReady event?

albell commented 11 years ago

No, loading conventionally via script tags at the bottom of the body. First cycle, then the various cycle2 plugins. I don't really see the point of wrapping everything in .ready() unless you're loading scripts are the head, or using require... Why?

malsup commented 11 years ago

I need more information about how the event is being missed. As written, the center plugin will always hear the cycle-pre-initialize event if your scripts are simply included statically on the page:

<script src="jquery.cycle2.js"></script>
<script src="jquery.cycle2.center.js"></script>

The last thing the cycle2 script does is register a domReady listener. The first thing the center plugin does is register a cycle-pre-initialize listener.