jqueryminifyajaxmin

Error with eval while minification


I am facing an error while minification of scripts using ajaxmin which contains 'eval'. In the below statement , i am receiving an error 'grid is undefined'. The variable 'grid' has been changed to someother name after minification. I have resolved this issue storing the grid object in window like 'window.grid'. But is there someother ways to resolve this issue?

        $(form).html($("#" + this._gridID + "_bulkEditTemplate #" + colName + "_bulkEdit").html());           
        $(target).append(form);
        var grid = $find(this._gridID);
        var optionsObj = eval("grid.jsonModeMgr._jsonDataAndOption." + colName + "_Options");
        $("#" + this._gridID + " #" + colName).html($($("#" + this._gridID + "_bulkEditTemplate").tmpl(optionsObj).find('#' + colName + ' option')));
        var editElement = $("#" + this._gridID + " #" + colName);
        $(editElement).val(this.currentCellValue == null ? "" : this.currentCellValue);
        $(editElement).val(this.currentCellValue).focus();
        $(editElement)[0].focus();

Thanks


Solution

  • In general, you should avoid eval() when possible. You can replace this eval() line:

    var optionsObj = eval("grid.jsonModeMgr._jsonDataAndOption." + colName + "_Options");
    

    with this:

    var optionsObj = grid.jsonModeMgr._jsonDataAndOption[colName + "_Options"];
    

    To access a property by a calculated string name or a string contained in a variable, you can use the [string here] syntax instead of the dot syntax.