Problem with queued events (Ajax load) in my plugin
Here is the code (my questions at the bottom):
- ;(function($) {
- $.fn.tq_contentslider = function(o) {
- var options = $.extend({}, $.fn.tq_contentslider.defaults, o);
-
- return this.each(function() {
- //get metadata if plugin available
- var opts = $.metadata ? $.extend(options, $.metadata.get(this)): options;
- //set initial menu and content display on page load
- var $menubar = $(this);
- var $container = $('#'+opts.container_id);
- var hash = '';
- path = window.location.href.split('/');
- hash = path[path.length - 1].split('.')[0];
- if(hash.length && $('li a#tq_menu_'+hash, $menubar).length) {
- $('li a#tq_menu_'+hash, $menubar).addClass('current');
- $('div#'+opts.container_id+'_'+hash, $container).addClass('current');
- } else {
- $('li a:first', $menubar).addClass('current');
- $('div:first', $container).addClass('current');
- }
- //set events for menu elements
- $('li a', $menubar)
- .click(function() {
- if(!$(this).is('.current') && !$container.is(':animated')) {
- var $this = $(this);
- hash = $this.attr('href').split('#')[1];
- $content = $('div#'+opts.container_id+'_'+hash);
- //onStart callback
- opts.onStart($this, $container, $content, hash, opts);
- //remove and set menu current classes
- $('li a.current', $menubar).removeClass('current').css({backgroundPosition: '0px 0px'});
- $this.addClass('current').css({backgroundPosition: '0px -'+(opts.menu_height * (opts.menu_sprites - 1))+'px'});
- //animate and set content current classes
- $container.animate({height: '5px'}, opts.speed, opts.easing, function() {
- $('div.current', $container).removeClass('current');
- //onHide callback
- opts.onHide($this, $container, $content, hash, opts);
- $container.animate({height: $content.addClass('current').outerHeight()}, opts.speed, opts.easing, function() {
- //onEnd callback
- opts.onEnd($this, $container, $content, hash, opts);
- });
- });
- }
- return false;
- })
- .mouseover(function() {
- if(!$(this).is('.current')) {
- $(this).css({backgroundPosition: '0px -'+(opts.menu_sprites > 1 ? opts.menu_height : 0)+'px'});
- }
- })
- .mouseout(function() {
- if(!$(this).is('.current')) {
- $(this).css({backgroundPosition: '0px 0px'});
- }
- });
- });
- }
- })(jQuery);
- $.fn.tq_contentslider.defaults = {
- container_id: 'tq_content',
- speed: 1000,
- menu_sprites: 3,
- menu_height: 55,
- easing: 'swing',
- onStart: function() {},
- onHide: function() {},
- onEnd: function() {}
- };
- function sliderOnHide($menu, $container, $content, hash, opts) {
- //check whether to perform ajax
- if($content.html().length < 10 || $('h1 cufontext:first', $content).html() == 'Error') {
- $content.load('ajax_main.php', {hash: hash}, function(resp, status, xhr) {
- if(status == 'error') {
- $content.html('<h1>Error</h1><p>A network error has occurred, likely due to network congestion. The content could not be loaded. Please try again.<p>');
- $menu.removeClass('current').css({backgroundPosition: '0px 0px'});
- }
- Cufon.replace('div#'+opts.container_id+'_'+hash+' h1');
- });
- }
- }
The problem is lines 37 and 38. opts.onHide calls the callback function sliderOnHide (which performs an Ajax load call) at the bottom of the listing. The problem is that line 38 starts executing before line 37 completes, thus line 38 cannot calculate a proper outerHeight.
Any suggestions on how to ensure line 37 completes before line 38 begins too do so?
(If you have any other tips for the rest of the code, feel free to pass it on. This is my first plugin. Using it to get a feel for jQuery.)
Thanks,
M.