使用必需参数和可选参数构建对象

时间:2012-09-27 22:23:13

标签: javascript jquery jqgrid

我正在尝试创建一个网格构建器对象。这个网格构建器有一个方法buildGrid,我设计的方法是期望一个对象为网格定义一堆参数:

buildOrdersGrid: function () {
    var ordersGrid = buildGrid({
        gridElementID: 'OrdersGrid',
        gridPagerElementID: 'OrdersGridPager',
        colNames: ['Order ID', 'Project Subcode', 'Incident Number', 'Cost Center', 'Name', 'Customer'],
        colModel: [
            { name: 'ID', hidden: true },
            { name: 'ProjectSubcode' },
            { name: 'IncidentNumber' },
            { name: 'CostCenter' },
            { name: 'Name' },
            { name: 'Customer' }
        ],
        defaultCaption:'Orders: no filter applied',
    });
    return ordersGrid;
}

function buildGrid(data) {
    var grid = $('#' + data.gridElementID);
    var gridPager = $('#' + data.gridPagerElementID);

    grid.jqGrid({
        datatype: 'local',
        colNames: data.colNames,
        colModel: data.colModel,
        gridview: true,
        height: 'auto',
        pager: gridPager,
        viewrecords: true,
        multiselect: true,
        defaultCaption: data.defaultCaption,
        caption: data.defaultCaption,
        shrinkToFit: false
    });

    return grid;
}

类似的东西,但它确实是新代码,因此可以提供有关如何改进的建议。

现在,我想扩展这个buildGrid方法,以便它可以获取非预定义属性并将它们提供给jqGrid。类似的东西:

buildTaskGrid: function () {
    var tasksGrid = buildGrid({
        gridElementID: 'TasksGrid',
        gridPagerElementID: 'TasksGridPager',
        colNames: ['Order', 'Task ID', 'Task #', 'Type', 'Status', 'Assignee', 'Current Location', 'Dest Location', 'Change No', 'Net Patched', 'SAN Patched'],
        colModel: [
            { name: 'Order' },
            { name: 'TaskID', hidden: true },
            { name: 'TaskNo' },
            { name: 'Type' },
            { name: 'Status' },
            { name: 'Assignee' },
            { name: 'CurrentLocation' },
            { name: 'DestLocation' },
            { name: 'ChangeNo' },
            { name: 'NetPatched' },
            { name: 'SANPatched' }
        ],
        defaultCaption:'Tasks: no filter applied',
        //Decorate with task-specific properties.
        grouping: true,
        groupingView: {
            groupField: ['Order'],
            groupColumnShow: [false]
        },
        ondblClickRow: function (rowid) {
            $(this).trigger('taskDoubleClicked', selector.getRowData(rowid));
        } 
    });

    return tasksGrid;
}

我不确定我应该如何最好地'找到'意料之外的属性并将它们交给网格。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我认为您可以使用此http://api.jquery.com/jQuery.extend/