0

更新 我意识到这个问题在实施的情况下很难理解。要查看和测试工作示例,请下载解决方案。

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


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

  • Reportwriter.js 与 html 的基本绑定以显示模式
  • ReportWriterEffects.js 这包含 UI 生成的任何事件,这些事件会传递给淘汰视图模型
  • ReportWriterUtilities.js。这包含 Viewmodel 中的数据/对象操作函数
  • ReportWriterViewModel.js ReportWriter 视图模型 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);
            }

它将我的可观察报告传递给进行 ajax 调用的 reportWriterUtilities.js 文件

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的想法可能会有所帮助。依然没有。

我添加了警报消息,它确实被调用了,但是 context.$root.Save(); 永远不会被调用。

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

有没有人有任何建议或者你过去有没有经历过这样的事情。我会很感激任何洞察可能导致这种情况发生的原因。我希望下面的代码就足够了。我不能使用小提琴,因为我在这里使用了显示模式并且它没有给出代码的准确表示。

4

1 回答 1

4

在 IE8 和 9 中,console当 F12 开发人员工具尚未为当前文档初始化时,它是未定义的(afaik,旧版本的 firebug 也有此问题)。

self.Save似乎console.log('VM reached');ReportWriterViewModel.js(13)中失败了。也可能有其他地方,但这是我的工匠静态代码分析所获得的(无法访问 VisualStudio IDE ......或信任未经验证的解决方案:)

只要console从生产代码中剥离调用,您就可以在开发人员工具打开的情况下继续照常开发业务——这将是我个人的偏好。

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

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

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

于 2013-11-25T01:59:08.477 回答