在IE9中未检测到视图模型函数绑定,但在FF和Chrome中有效

View Model Function binding not detected in IE9 but works in FF and Chrome

本文关键字:但在 FF Chrome 有效 绑定 模型 IE9 检测 视图 函数      更新时间:2023-09-26

更新我意识到这个问题在实施过程中很难理解。要查看和测试工作样本,请下载解决方案。

要进行测试,您必须使用IE9,并且开发工具不能打开。打开开发工具将使项目按预期工作。此解决方案适用于任何其他浏览器。为了进行测试,我在IIS中本地设置了解决方案,并在VS/debug之外运行它。


我有一个项目,我试图将一个淘汰赛的可观察结果保存回服务器。我在IE9中看到了一些非常奇怪的事情,我无法解释,我希望有人能帮助我。我正在使用Asp.NET Mvc和knockout,并为项目的这一部分实现了JS揭示模块模式。我的js文件如下:

  • Reportwriter.js与html的基本绑定,用于揭示模式
  • ReportWriterEffects.js它包含UI生成的传递给淘汰Viewmodel的任何事件
  • ReportWriterUtilities.js。它包含用于在Viewmodel中操作数据/对象的函数
  • ReportWriterViewModel.js Reportwriter viewmodel Json序列化/序列化

我的html页面有以下元素

<a id="footerSaveButton" class="blackGradient panelBtn pleft" >

单击此按钮时,会从ReportWriterEffects.js 中调用一个非侵入性事件

 $(document).on({
            click: function () {
                alert('click');// placed here for testing
                var context = ko.contextFor(this);
                context.$root.Save();
            }
        }, '#footerSaveButton');

这应该反过来从看起来像的ReportWriterViewModel.js调用

self.Save = function () {
            if (self.ValidateReport()) {
                // Transform the custom types in the myFilters collection into the form required by the server-side representation.
                self.TransformFilters();
                ReportWriterUtilities.Save(self.iqReport);
            }

它将我的报告observable传递到reportWriterUtilities.js文件,在该文件中进行ajax调用

function Save(reportObservable) {
  // Determine which method to call based upon the state of report modification.
  var command = reportObservable().IsNewReport() ? "SaveNewReport" : "UpdateExistingReport";
$.ajax({
    type: "POST",
    url: "../" + command,
    data: "{ json:'" +
    ko.toJSON(reportObservable, function (key, value) { // Remove the __type attribute. This causes problems with deserialization.
        if (key == "__type") {
            return;
        }
        else {
            return value;
        }
    }) + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    cache: false,
    success: function (msg) {
        if (msg.Key != null) {
            // Update the ViewModel with properties from the saved Report.
            reportObservable().ReportId(msg.Key.ReportId);
            reportObservable().DateLastModified(msg.Key.DateLastModified);
            AlertMessaging.DisplaySuccess("Report Saved!", 'Your Report has been saved and is available in your Report Library. <i><a target="_self" href="../../ReportLibrary">Go to Report Library</a></i>');
        }
        else {
            // Display an error with the given text.
            AlertMessaging.DisplayError("Problem Saving Report", msg.Value);
        }
    }, error: function (err) {
        AlertMessaging.DisplayError("Error Saving Report", "The Server returned an error code. Code: " + err.status);
    }
    });
}

在Firefox和Chrome中,这一切都有效,并且报告会被保存。然而,在IE中,保存调用永远不会被调用。我最初在<a>元素上有一个敲除绑定集,它从未被调用过。我认为浏览器缓存可能已经对此产生了影响,因为我使用Mvc,所以我通过向控制器构造函数添加以下属性来装饰控制器,使其不加载缓存

[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public class ReportLibraryController : RootController
{ ...

仍然没有骰子。

如果您查看ajax调用,您还会看到我在哪里添加了cache: false,这可能会有所帮助。还是什么都没有。

我添加了警报消息,但调用了上下文$根Save();从未被调用。

现在是一件非常奇怪的事情当我在IE9中加载页面并按F12打开IE中的开发工具,然后关闭开发工具时,会调用视图模型中的保存函数,一切正常。如果我启动浏览器打开开发工具并导航到页面,一切都会正常工作。控制台中不会生成任何错误或消息,并且在其他浏览器中运行良好。这似乎与IE9无关,因为IE10运行良好。

有人有什么建议吗?或者你过去经历过这样的事情吗。如果能深入了解导致这种情况发生的原因,我将不胜感激。我希望下面的代码就足够了。我不能使用fiddle,因为我在这里使用的是揭示模式,它不能准确地表示代码。

在IE8&9,每当F12开发人员工具没有为当前文档初始化时,console都是未定义的(afaik,firebug的旧版本也会受到此影响)。

您的self.SaveReportWriterViewModel.js(13)中的console.log('VM reached');上似乎失败了。可能还有其他地方,但这是我的手工静态代码分析所能达到的(无法访问VisualStudioIDE…或信任未经验证的解决方案:)

只要从生产代码中去除console调用,您就可以在开放开发工具的情况下照常开发业务——这是我个人的偏好。

或者,您可以选择用noop(一种什么都不做的方法)"插入"控制台方法:

/*https://stackoverflow.com/a/9593812/1081234*/
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };

这个答案也值得注意,因为它插入了其他控制台方法。

相关文章: