Underscore.js模板仅在IE 8/9中引发语法错误

时间:2013-08-10 00:24:49

标签: javascript underscore.js

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

错误具体是:

SCRIPT1002: Syntax Error

指向在underscore.js中_template函数末尾的try / catch调用的 throw e; 行。 (这是注释为如果未指定变量的部分,将数据值放在本地范围内)现在我无法想象为什么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/

感谢。

1 个答案:

答案 0 :(得分:3)

看起来<script id="projectTemplate"

中有一个错误的关闭蜜蜂字符串
<img src=<%= mainImg %> alt="<%= title &>" 
                                       ^---- Supposed to be %

另外请不要忘记src的引用(IE也不喜欢这样)

如果这不是故意或拼写错误。通常IE8 / IE9在这种拼写错误中表现得很糟糕。