我目前有一個大的外部JavaScript文件,在頁面上使用。我目前將代碼包裝在一個自調用函數中,因爲我有其他部分使用ajax選項卡加載,所以我想避免與其他外部js文件命名衝突。如何將JavaScript文件組織成更小的片段?
該文件中的代碼組織如下。我想將plannerTab命名空間中的一些代碼拆分爲更小的文件,但它仍然是該命名空間的一部分。
我該怎麼做?或者,你們是否推薦了一種不同的方法?謝謝!
// Document Ready
$(function()
{
// initializes table
plannerTab.plannerTable.init();
});
var plannerTab = (function()
{
// All the code for the page is in here. I would like to extract sections
// from in here and put them into their own external files while still keeping
// the namespacing
}();
更新
我怎麼會從plannerTab變量中獨立的部分到更小的外部JS文件,並仍然認爲他們是plannerTab命名空間的一部分嗎?下面是一個小例子。
// Scope: plannerTab.config - Would like to store configuartion into a separate file
var config = {
selectors: {
tableId: '#plannerTable',
addTaskId: '#AddTask',
editTaskSelector: '#plannerTable .edit',
dateFilterSelector: '#plannerTable_TimeFilter li',
deleteTaskClass: '.delete',
searchFilter: '#plannerTable_filter',
selectedDateFilter: 'selected-dateFilter',
taskCellSelector: '#plannerTable .task-col',
taskClass: '.taskId'
},
urls: {
addTaskFormURL: '/Planner/Planner/LoadAddTaskForm',
editTaskFormURL: '/Planner/Planner/LoadEditTaskForm',
deleteTaskURL: '/Planner/Planner/DeleteTask',
getTasksByDateRangeURL: '/Planner/Planner/GetTasksByDateRange',
viewTaskURL: '/Planner/Planner/ViewTask'
}
};
這是爲了簡化開發還是部署?我建議你保留一個文件,以減少HTTP請求。 – 2011-02-15 00:16:10