1

我有这段代码(也显示在下面),它在 IE8 中给我一个错误,但在 Chrome 和 PhantomJS 中很好。

错误是“对象不支持此属性或方法knockout-2.2.1.debug.js,第2319行字符35”,调用自currentPage(pages[pages.indexOf(current) + steps]);

我不知道为什么它不工作,所以任何帮助将不胜感激!

var Page = (function () {
    function Page(index, name, canNavigateToPage, navigatedToThisPage) {
        this.index = index;
        this.name = name;
        this.canNavigateToPage = canNavigateToPage;
        this.navigatedToThisPage = navigatedToThisPage;
    }
    Page.prototype.navigateToPage = function () {
        if (this.canNavigateToPage()) {
            this.navigatedToThisPage(this);
        }
    };
    return Page;
})();

var AccountSearchParameters = (function () {
    function AccountSearchParameters() {
        this.reference = ko.observable();
        this.scheme = ko.observable();
        this.lastName = ko.observable();
        this.sufficientInputToSearchForAccount = ko.computed(function () {

            return this.reference() && this.scheme() && this.lastName();
        }, this);
    }
    return AccountSearchParameters;
})();

function viewModel() {
    var self = this,
        currentPage = ko.observable(),
        accountSearchParameters = new AccountSearchParameters(),
        forwardPageProgressionGuards = {
            '1': function canMoveToPage2() {
                return accountSearchParameters.sufficientInputToSearchForAccount();
            },
                '2': function canMoveToPage3() {
                return true;
            },
                '3': function canMoveToPage4() {
                return true;
            }
        },
        canMoveToNextPage = function (currentlyOnPage) {
            function disallowPageMovementNotExplicitlyDefined() {
                return false;
            }

            return (forwardPageProgressionGuards[currentlyOnPage] || disallowPageMovementNotExplicitlyDefined)();
        },
        canMoveToPreviousPage = function (currentlyOnPage) {
            return currentlyOnPage > 1;
        },
        pages = [
        new Page(1, 'Customer details', function () {
            return true;
        }, function (page) {
            currentPage(page);
        }),
        new Page(2, 'Bank details', forwardPageProgressionGuards['1'], currentPage),
        new Page(3, 'Payment details', forwardPageProgressionGuards['2'], currentPage),
        new Page(4, 'Confirmation', function () {
            return true;
        }, currentPage)],
        pageNavigator = function (canNavigate, steps) {
            current = currentPage();
            console.log(canNavigate(current.index));
            if (canNavigate(current.index)) {
                currentPage(pages[pages.indexOf(current) + steps]);
            }
        };

    currentPage(pages[0]);

    self.page = ko.computed(function () {
        return currentPage();
    });

    self.accountSearchParameters = accountSearchParameters;
    self.nextPage = function () {

        pageNavigator(canMoveToNextPage, 1);
    };
    self.previousPage = function () {
        pageNavigator(canMoveToPreviousPage, -1);
    };

    self.canMoveToNext = ko.computed(function () {
        return canMoveToNextPage(currentPage().index);
    });

    return self;
}

$(function () {
    ko.applyBindings(viewModel());
});
4

1 回答 1

2

IE8 不支持 indexOf,使用 $.inArray

于 2013-06-10T13:02:59.830 回答