I have several links that points to a single AJAX url. I can load it into a single #DIV, for each request with patterns: load and remove for each link. Now since all I need is a single url, I am trying to avoid removal and re-request procedure.
I have two scenarios, still questioning and badly need suggestions to also handle them:
Create different container #DIVS so they won't conflict when loading this single url into each DIV and proceed with regular hiding and showing (without destructing/removal). This means each link request the same url and load the same AJAX into each container #DIV. Very consumptive, since each link loads the same AJAX again and again.
Create just a single #DIV and MOVE AROUND this #DIV into another .DIV (the same containing class, say .ajax-container).
So what I am trying to get is reusable and efficient code here. Any suggestions and examples would be very much appreciated.
UPDATE, added what has been done so far to clarify my question:
$('a.browse')
.click(function(){
var t = $(this);
t.show();
t.parent().prev().find('input.tex').addClass('focus');
// @TODO, avoid using remove(), and move this into another .hasPicker class instead to REUSE the same content
$('#imgPicker,#closebox').remove();
$('.picker-on').removeClass('picker-on');
t.parent().prev().find('input.tex').after('<div id="imgPicker"></div>').parent().addClass('picker-on');
// @TODO, Check if #tabs has been loaded somewhere, and try to append it into current link that calls it
if ($('#tabs').length) {
t.parent().prev().find('#imgPicker').append('#tabs');
} else {
t.parent().prev().doAjax(); // load the same AJAX content
}
t.hide();
return false;
});
$('input.tex').wrap('<div class="hasPicker"></div>');
And here is the doAjax() stuff:
$.fn.doAjax = function() {
return this.each(function() {
// Check for pending request & cancel it
if (req) req.abort ();
//showLoading();
req = $.ajax ({
url: "?ajax=1",
method:'GET',
cache: true,
success: function (data) {
$('#imgPicker').html(data).show().after('<span id="closebox"></span>');
$('#closebox').show();
$("#tabs").tabs();
// reset request
req = null;
},
error:function() {
alert('Fail to load image collection.');
}
});
});
};
Notes: #imgPicker is a dynamic container for #tabs. I used to have a ui.dialog, but seeing the size then I accomplished it with just a simple custom popup position absolute against the link and form requesting it. doAjax() is handled separately as a single request to load the same content (#tabs). input.tex is a set of textfield forms (where all items/options in #tabs can be stored.) followed by their link that request the same AJAX content via the same AJAX url.
Hope I can make myself clear.
Thanks