0

我有一个问题,我无法访问函数内的变量:

编辑 我忘了补充说我workerPage.grid = $("#grid").data("kendoGrid");在 jQuery 上设置了这个$(function(){});

我不能claimsGird在保存函数中使用变量,我必须通过workerPage.grid. 不是像其他变量一样viewModel工作正常。这是片段:

save = function () {
                saif.kendoGridUtils.addModifiedDataItems(
                    viewModel.CompanionClaims.Updated,
                    viewModel.CompanionClaims.Added,
                    $("#grid").data("kendoGrid").dataSource.data()
                );

                $.ajax({
                    url: $("#contentForm").attr("action"),
                    data: JSON.stringify(viewModel),
                    type: "POST",
                    contentType: "application/json"
                }).success(function (data) {
                    //syncs viewModel with changes in model
                    $.extend(viewModel, kendo.observable(data));

                    //rebinds the grid data source
                    claimsGrid.dataSource.data(viewModel.CompanionClaims.Rows);

这是完整的脚本:

var workerPage = (function () {
        var viewModel = kendo.observable(@Html.Raw(Json.Encode(Model))),
            claimsGrid = null,
            deleteFirm = function (firmModel) {
                firmModel.Name = "";
                firmModel.AttorneyName = "";
                firmModel.Address.Line1 = "";
                firmModel.Address.Line2 = "";
                firmModel.Address.City = "";
                firmModel.Address.State = "OR";
                firmModel.Address.ZipCode = "";
                firmModel.Address.PlusFourCode = "";
                firmModel.PhoneNumber = "";
                firmModel.FaxNumber = "";
                firmModel.ContactName = "";
            },
            bind = function () {
                kendo.bind($("#main-content"), viewModel);
            },
            save = function () {
                saif.kendoGridUtils.addModifiedDataItems(
                    viewModel.CompanionClaims.Updated,
                    viewModel.CompanionClaims.Added,
                    $("#grid").data("kendoGrid").dataSource.data()
                );

                $.ajax({
                    url: $("#contentForm").attr("action"),
                    data: JSON.stringify(viewModel),
                    type: "POST",
                    contentType: "application/json"
                }).success(function (data) {
                    //syncs viewModel with changes in model
                    $.extend(viewModel, kendo.observable(data));

                    //rebinds the grid data source
                    claimsGrid.dataSource.data(viewModel.CompanionClaims.Rows);


                    //rebinds view elements to view model so changes are visible
                    //kendo.bind($("#main-content"), viewModel);
                    bind();

                    // Errors and Warnings
                    var results = messageUtils.parseMessages(
                        viewModel.Messages.Errors,
                        viewModel.Messages.Informationals,
                        viewModel.Messages.Warnings
                    );

                    var errs = $("#errors").html(results.errorMessages);
                    $("#informationals").html(results.informationalMessages);
                    $("#warnings").html(results.warningMessages);

                    $.each(saif.kendoGridUtils.processErrors(viewModel.CompanionClaims.Rows), function (i, message) {
                        errs.html(errs.html() + message + "<br>");
                    });
                    // End Errors and Warnings

                });
            },
            deleteRow = function () {
                var row = claimsGrid.select(),
                    rowDataItem = claimsGrid.dataItem(row),
                    rowIndex = $(row).index(),
                    addedItemIndex = $.inArray(rowDataItem, viewModel.CompanionClaims.Added);

                //add to Deleted if not new
                if (addedItemIndex == -1 && $.inArray(rowDataItem, viewModel.CompanionClaims.Rows) != -1) {
                    viewModel.CompanionClaims.Deleted.push(rowDataItem);
                }

                //remove from Added if exists
                if (addedItemIndex != -1) {
                    viewModel.CompanionClaims.Added.splice(addedItemIndex, 1);
                }

                claimsGrid.removeRow(row);

                //select the next row, eg. if you delete row 2, select the row that took that rows poisition after it was deleted.
                claimsGrid.select(claimsGrid.tbody.find(">tr:eq(" + rowIndex + ")"));
            };

        return {
            bind: bind,
            deleteFirm: deleteFirm,
            deleteRow: deleteRow,
            grid: claimsGrid,
            save: save,
            viewModel: viewModel
        };

    }());
4

2 回答 2

2

问题是它claimsGrid永远不会设置为null. 并且设置workerPage.grid不会改变claimsGrid它的值——它不是一个指针,只是一个副本。

您将不得不使用 getter/setter。使用较新的浏览器/引擎,可以使用getand来完成set

// ...
return {
    // ...
    get grid() {
        return claimsGrid;
    },
    set grid(grid) {
        claimsGrid = grid;
    },
    // ...
};

您还可以定义grid为函数:

// ...
function getOrSetGrid(grid) {
    if (typeof newGrid === 'undefined') {
        return claimsGrid;
    } else {
        claimsGrid = grid;
    }
}

return {
    // ...,
    grid: getOrSetGrid,
    // ...
};
// ...
// rather than: workerPage.grid = ...;
workerPage.grid(...);

或将其拆分为getGridsetGrid功能。

于 2013-01-23T19:48:41.397 回答
0

javascript 中的作用域与 Java 或 C# 等其他语言的工作方式不同。在您的情况下,claimsGrid 不在保存功能的范围内。这有帮助吗?http://coding.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/

于 2013-01-23T19:48:10.640 回答