3

我正在处理的网站正在使用主干和下划线从 JSON 文件中提取的数据中呈现页面元素。在我将 IE 设置为在 IE 9 的兼容模式下进行测试之前,所有浏览器的一切都运行良好且花花公子。(我没有在本机 IE 9 环境中进行测试 - 而是使用 IE 10 开发人员窗口并为 IE9 和 Document 设置浏览器模式IE9 标准的模式)。

错误具体是:

SCRIPT1002: Syntax Error

带有指向throw e 的指针;underscore.js 中 _template 函数末尾的 try/catch 调用行。(这是注释为如果未指定变量,则将数据值放在本地范围内的部分)现在我无法想象为什么 throw 调用会产生实际错误,所以我假设在某处存在问题在 try/catch 中抛出错误,并且 IE 正在返回该错误,而不是实际发生错误的行。

该部分的完整来源是:

// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';

source = "var __t,__p='',__j=Array.prototype.join," +
  "print=function(){__p+=__j.call(arguments,'');};\n" +
  source + "return __p;\n";

try {
  render = new Function(settings.variable || 'obj', '_', source);
} catch (e) {
  e.source = source;
  throw e;
}

我花了很多时间在 IE 开发人员窗口中用断点逐行筛选代码,但是在发生错误时,我不知道 jquery / underscore 实际在做什么,除了模板正在调用来渲染元素。到目前为止,我在网上找到的唯一解决方案是人们在他们的 JSON 中使用保留字作为键,但对我来说似乎并非如此。

单个对象是 JSON 文件,如下所示:

{ 
    "id": "1",
    "title": "Project Title",
    "client": "Client Name",
    "scope": "Design and Producion",
    "projectCode": "homeEnt",
    "projectType": "Home Entertainment",
    "description": "Blah blah blah",
    "video" : "false",
    "mainImg": "images/gallery-he/project.jpg",
    "fullImg": "images/gallery-he/project-full.jpg"
}

HTML 中使用的两种类型的模板是:

<script id="projectTemplate" type="text/template">
  <div class="<%= projectCode %>-box" rel="<%= id %>">
      <div class="gradient-strip <%= projectCode %>" ><%= projectType %></div>
      <img src=<%= mainImg %> alt="<%= title &>" class="project-img">
  </div>
</script>

<script id="projectModel" type="text/template">
  <div class="model-frame" rel="<%= id %>" style="display:none">
    <div class="model-gradient <%= projectCode %>"><%= projectType %></div>
    <div class="close-button"></div>

    <a class="model-left-arrow"></a>
    <img class="fullArt" src="<%= fullImg %>" alt ="<%= title %>" />
    <a class="model-right-arrow"></a>

    <div class="model-text-block">
        <p class="model-text"><span class="bolded"><%= title %> </span></p>
        <p class="model-text"><span class="bolded">Client: </span> <%= client %></p>
        <p class="model-text" style="padding-bottom:10px;"><%= scope %></p>
        <p class="model-text"><%= description %></p>
    </div>

  </div>
</script>

即使有一些选择性的评论,我也无法推断出其中哪一个是问题孩子,因为评论一个会破坏另一个,反之亦然。

最后,查看代码:

var ProjectModelView = Backbone.View.extend({
    tagName: "div",
    className: "model-wrap",
    events: {
        // events are here, removed to save space 
    },
    initialize: function() {
        this.template = _.template($(this.options.templ).html());
    },
    render: function(){
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    },
    // added functions are here (removed to save space)
  });

  var ProjectView = Backbone.View.extend({
    tagName: "div",
    className: "project-wrap",
    initialize: function () {
        this.template = _.template($('#projectTemplate').html());
    },
    render: function () {
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    },
    events: {
        // events...
    },
    // added functions...
  });

  var ProjectModelListView = Backbone.View.extend({
    el: '#modelList',
    initialize: function() {
      this.collection = masterProjectList;
      this.render();
    },
    render: function() {
      this.$el.html("");
      this.collection.each(function(project) {
            this.renderItem(project);
      }, this);
    },
    renderItem: function(project) {
      var isVideo = project.get('video');
      if (isVideo == "true") {  
        var projectModelView = new ProjectModelView({ model: project, templ: '#videoProjectModel' });
        this.$el.append(projectModelView.render().el);
      } else {
        var projectModelView = new ProjectModelView({ model: project, templ: '#projectModel'});
        this.$el.append(projectModelView.render().el);
      }
    }
  });

  var ProjectListView = Backbone.View.extend({
    el: '#projectList',
    initialize: function() {
      this.collection = masterProjectList;
      this.render();
    },
    render: function() {
      this.$el.html("");
      this.collection.each(function(project) {
            this.renderItem(project);
      }, this);
    },
    renderItem: function(project) {
      var projectView = new ProjectView({ model: project });
      this.$el.append(projectView.render().el);
    }
  });

对不起,这是这么多的文字,但我完全不知道是什么方面导致它在 IE 8 / 9 中失败,所以我不确定我需要具体修改什么。有什么想法会让下划线在特定的浏览器版本中变得疯狂吗?

您可以在此处查看该网站:

http://www.thecrpgroup.com/crpweb/promo-site/

谢谢。

4

1 回答 1

3

看起来有一个错误的闭合蜂弦<script id="projectTemplate"

<img src=<%= mainImg %> alt="<%= title &>" 
                                       ^---- Supposed to be %

也不要忘记引号src(IE也不喜欢那样)

如果那不是故意的或错字。通常 IE8/IE9 对此类拼写错误的表现非常糟糕。

于 2013-08-10T00:38:32.407 回答