2009-12-31 88 views
1

讓我們開始說我的代碼工作得很好,沒有問題。 我只想將它包裝好,以便將來可以充分利用,並且我想知道如何以最正確的方式在Javascript中執行此操作。創建一個具有動態功能的Javascript類

load_server是一個放入Ajax請求的函數。 分頁()/ itemlinks()是通過檢索的數據並標記未來Ajax使用的鏈接的函數。 我目前在功能寫的一切,只是轉儲它的代碼,但我想創造的東西,如:

function ajaxRequest(type, link, container) { 
    this.after_success = null; 
    this.check_pagination = true; 
    if(typeof(type)) == ‘undefined’) this.type=''; 
    if(typeof(link)) == ‘undefined’) this.link=''; 
    if(typeof(container)) == ‘undefined’) this.container=''; 

    this.pagination=function() { 
     //static function.. 
    }; 
    this.load_server=function() { 
     //function logic here.. 
    }; 
    while(mainReq.after_success) { 
     func(); 
    } 
} 

var mainReq = new ajaxRequest{'link'}; 
mainReq.after_success = { 
    itemlinks = function() { }, 
    morefunc = function() { } 
}; 
mainReq.submit(); 

我目前使用下面的jQuery代碼:

load_server = function (type, link, container) { 
$(container).html("<div class='mask-loading'>Loading ...</div>"); 

$(container).load(getUrl, 
function(responseText, textStatus, XMLHttpRequest) { //Callback function. 
    if(textStatus=='success') { 
     pagination_render(); 
     itemlinks(); 
    } 
    else { 
     $(container).html("We are sorry, but there was an error with your request.<br />Please try again."); 
    } 
}); 
} 
var pagination_render = function() { 
    var pagination = $('.pagination a'); 
    pagination.each(function() { 
     $(this).click(function(event) { 
      console.log(this.href); 
      load_server('page', this.href, '#tab1'); 
      return false; 
     }); 
    }); 
}; 
pagination_render(); //init any pagination files 

的其他功能相同,所以不需要將它們四處飛濺。

回答