Not some much a problem, I am curious to know if my code can be cleaned up here a little. One annoyance is im calling a function three ( 3 ) times. I would like to know how this could be improved, ie: less calls to the function.
loadComments is the function being called in this particular object, however other object functions are similar.
For instance when loadlist is called it will run the function initially, then call it again at set interval, how can I improve this?
Regards, Phil
(function($){
/**
* =============================================================================================
* Load pagination li's into a scope(table id)
* =============================================================================================
*
*/
var loadPagination = function(scope, pag){
var pagination = $(scope).find("ul.pagination");
pagination.empty();
pagination.append(pag);
}
/**
* =============================================================================================
* Load table body rows into a scope(table id)
* =============================================================================================
*
*/
var loadTableBody = function(scope, data){
var tblBody = $(scope).find("tbody");
tblBody.empty();
tblBody.append(data);
}
/**
* =============================================================================================
* load ajax loading icon
* =============================================================================================
*
*/
var loadLoader = function(scope){
var loadingSpan = $(scope).find("tbody"),
loadingIcon = $("<span class='loader'> </span>"),
span = $("<td style='text-align:center' colspan='8'>");
loadingSpan.empty();
span.append(loadingIcon).appendTo(loadingSpan);
}
/**
* =============================================================================================
* load the list function at set interval
* =============================================================================================
*
*/
var loadList = function(func, interv){
func();
window.setInterval(function(){
func();
}, interv);
}
/**
* =============================================================================================
* Comments Object
* =============================================================================================
*
*/
var comments = {
init : function(){
if(document.getElementById("comments")){
this.loadCommentsList();
}
},
loadCommentsList : function(){
var scope = $("table#comments"),
tmplId = $("#comments-list"),
loadComments = function(offset){
var data = {'offset' : (offset) ? offset : 0}
$.ajax({
url : BASE_PATH + 'admin/comments/get',
type : 'POST',
data : data,
dataType : 'json',
beforeSend : function(){
loadLoader(scope);
},
success : function(cb){
window.setTimeout(function(){
loadTableBody(scope, tmplId.tmpl(cb.comments));
loadPagination(scope, cb.pagination);
}, 300);
}
});
}
//load initial comments list ( 1 ) AND call at interval ( 2 )
loadList(loadComments, 60000);
//listen for pagination click events and make ajax request ( 3 )
//TODO : remove function from pages object
pages.PaginationListner(loadComments, scope);
}
}
/**
* =============================================================================================
* Call each object's init function
* =============================================================================================
*
*/
$(function(){
comments.init();
});
})(jQuery);