【问题标题】:Revealing module pattern not working as expected显示模块模式未按预期工作
【发布时间】:2014-01-23 11:54:57
【问题描述】:

于是我做了一个小小的“jqgrid factory”,这是我第一次从零开始写一个显示模块的javascript diddy:

var jqGridReportFactory = (function () {

    var config = {
        datatype: 'json',
        mtype: 'GET',
        height: 'auto',
        autowidth: true,
        shrinkToFit: true,
        gridview: true,
        sortable: true,
        rowNum: 50,
        rowList: [50, 100, 200],
        viewrecords: true,
        loadonce: false,
        sortorder: 'asc',
        sortname: 'Affiliate',
        subGridSortname: 'SubAffiliate'
    },
    subGridOptions = {
        plusicon: "ui-icon-plus",
        minusicon: "ui-icon-minus",
        openicon: "ui-icon-carat-1-sw"
    },
    gridOptions = {
        id: null,
        pager: null,
        url: null,
        postData: null,
        colNames: null,
        colModel: null,
        pager: null,
        subGrid: false,
        subGridPostdata: null,
        subGridHeadersHidden: true
    };


    function createReport(gridOptions, optionalConfig) {
        $.extend(this.config, optionalConfig);
        $.extend(this.gridOptions, gridOptions);

        var jqGridObj = {
            url: this.url,
            datatype: this.config.datatype, //ERROR HERE!
            mtype: this.config.mtype,
            postData: this.gridOptions.postdata,
            colNames: this.gridOptions.colNames,
            colModel: this.gridOptions.colModel,
            height: this.config.height,
            autowidth: this.config.autowidth,
            shrinkToFit: this.config.shrinkToFit,
            gridview: this.config.gridview,
            sortable: this.config.sortable,
            rowNum: this.config.rownum,
            rowList: this.config.computeHighlightColorsrowList,
            viewrecords: this.config.viewrecords,
            loadonce: this.config.loadonce,
            sortorder: this.config.sortorder,
            sortname: this.gridOptions.sortname,
            pager: this.gridOptions.pager,
            loadError: function (xhr, st, err) {
                reportLoadError('onLoadConversionHistory', xhr, st, err);
                unblockUI();
            },
            gridComplete: function () {
                unblockUI();
                goToScrollPosition($('#reportPlaceHolder'));
            },
            subGrid: this.gridOptions.subGrid,
            subGridRowColapsed: function (subgrid_id, row_id) {
                // this function is called before removing the data 
                var subgrid_table_id;
                subgrid_table_id = subgrid_id + "_t"; //
                $("#" + subgrid_table_id).remove();
            },
            onSelectRow: function (rowid) {
                $(this).jqGrid("toggleSubGridRow", rowid);
            }
        };

        if (this.subGrid) {
            var subGridObj = {
                subGridOptions: this.subGridOptions,
                subGridRowExpanded: function (subgridId, rowId) {
                    var affiliate = $("#" + this.id).jqGrid("getCell", rowId, 'Affiliate');

                    var subgridTableId = subgridId + "_t";
                    var $subGrid;
                    $("#" + subgridId).html("<table id='" + subgridTableId + "' class='scroll'></table>");
                    $subGrid = $('#' + subgridTableId); //cache subgrid, more performant

                    //change parent names from Affiliate to Subaffiliate
                    //other than that subGrid model is exactly the same as parent Affiliate model for all reports so far
                    this.gridOptions.colNames[0] = 'Subaffiliate';
                    this.gridOptions.colModel[0].name = 'SubAffiliate';
                    this.gridOptions.colModel[0].index = 'SubAffiliate';

                    //add affiliate to subGridPostData
                    var a = { Affiliate: affiliate };

                    $.extend(this.gridOptions.subGridPostdata, a)
                    $subGrid.jqGrid({
                        url: this.gridOptions.url,
                        datatype: this.gridOptions.datatype,
                        mtype: this.gridOptions.mtype,
                        postData: this.gridOptions.subGridPostdata,
                        colNames: this.gridOptions.colNames,
                        colModel: this.gridOptions.colModel,
                        height: this.config.height,
                        sortname: this.config.subGridSortname,
                        sortorder: this.config.subGridSortorder,
                        loadonce: this.config.loadonce,
                        //these subgrid setting should not be overridden in my opinion - Brian Ogden
                        autowidth: true,
                        shrinkToFit: true,
                        gridview: false,
                        sortable: false,
                        viewrecords: true
                        ///////////////////////
                    });

                    if (subGridHeadersHidden) {
                        //hide subgrid column headers
                        $subGrid.closest("div.ui-jqgrid-view")
                        .children("div.ui-jqgrid-hdiv")
                        .hide();
                    }
                },
            };

            $.extend(jqGridObj, subGridObj);
        }

        //jqGrid factory go!!
        $("#" + this.gridOptions.id).jqGrid(jqGridObj);
    }

    return {
        createReport: createReport
    };

})();

jqGridReportFactory.createReport(continuityGridOptions);

出现错误:未捕获的类型错误:无法读取未定义的属性“数据类型”。我已经在上面的代码中注释了错误所在的行。我相信this 有问题,因此我设置第一个显示模块模式的方式有问题。

【问题讨论】:

    标签: javascript jquery jqgrid


    【解决方案1】:

    将行改为:

    datatype: config.datatype
    

    您不需要this,因为config 变量可以在createReport() 中完全访问。

    this.url 不抛出错误的原因很简单:它只是返回 undefined. However, trying to access another subproperty ofundefined` 最终会抛出错误。

    【讨论】:

    • 那么这怎么不是我所期望的,这不是=jqGridReportFactory吗?
    • @BrianOgden this 或多或少等于 {createReport: createReport}; - 您返回的对象。
    • 我明白了,谢谢。所以我的模块模式实现看起来没有什么时髦的?
    • @BrianOgden (对我来说) createReport() 似乎有点太长了(它可以分成几个函数)。考虑在CodeReview.SE 上发布您的代码。希望您能在那里得到更详细的答复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    相关资源
    最近更新 更多