json - underscore.js - 对象不支持此属性或方法 - ie8

标签 json backbone.js underscore.js

我有一个通过 require.js 运行的 Backbone /下划线应用程序

我刚刚对我的应用程序进行了一些跨浏览器检查,发现我的应用程序在 ie8 中加载模板时出现问题。

在我的应用程序和 View 部分中,当渲染函数运行时,我加载并调用了正确的模板

然后我使用 _.template 来转换代码。

所以在渲染中我称之为

 $t.$el.append($t.renderTemplate(data, templateVars));

数据是 <%=%> 格式的 html,templateVars 是返回数据的对象列表(通常是 json 结果)

renderTemplate 函数看起来像

 renderTemplate: function(html, vars) {
        var compiled = _.template(html);
        console.log(compiled, "compiled");
        return compiled(vars);
 },

问题是在ie8中我得到一个错误

对象不支持这个属性或方法

这是错误所在的行

return render.call(this, data, _);

来自这段代码

_.template = function(text, data, settings) {
settings = _.extend(_.templateSettings, settings);

// Compile the template source, taking care to escape characters that
// cannot be included in a string literal and then unescape them in code
// blocks.
var source = "__p+='" + text
  .replace(escaper, function(match) {
    return '\\' + escapes[match];
  })
  .replace(settings.escape || noMatch, function(match, code) {
    return "'+\n_.escape(" + unescape(code) + ")+\n'";
  })
  .replace(settings.interpolate || noMatch, function(match, code) {
    return "'+\n(" + unescape(code) + ")+\n'";
  })
  .replace(settings.evaluate || noMatch, function(match, code) {
    return "';\n" + unescape(code) + "\n;__p+='";
  }) + "';\n";

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

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

var render = new Function(settings.variable || 'obj', '_', source);
if (data) return render(data, _);
var template = function(data) {
  return render.call(this, data, _);
};

// Provide the compiled function source as a convenience for build time
// precompilation.
template.source = 'function(' + (settings.variable || 'obj') + '){\n' +
  source + '}';

return template;
};

// Add a "chain" function, which will delegate to the wrapper.
_.chain = function(obj) {
 return _(obj).chain();
};

这是 ff/chrome 用来转换传回模板的代码

// Add a "chain" function, which will delegate to the wrapper.
_.chain = function(obj) {
return _(obj).chain();
}; 

Underscore.js 1.3.2

这在 ie9、FF 和 chrome 中工作正常

谁能帮忙?

最佳答案

很可能是因为在 IE8 中不支持 console.log(),除非您打开 Dev Tools。

参见 this question

您是否检查过它是否适用于注释掉的 console.log() 行,或者在开发工具打开时?打开 Dev Tools 检查机智的好处是,您将能够看到错误发生在哪一行(如果它完全失败,因为 console.log() 将可用)。

关于json - underscore.js - 对象不支持此属性或方法 - ie8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17397574/

相关文章:

json - 处理IE且不处理fileSize等

java - TCP JSON 命令未到达

javascript - _createElement 方法的目的是什么?

javascript - 可以相互访问的节点的有效分组

json - 通过 JSON REST API (WP API) 联系表单 7?

mysql - 正确转义 JSON 特殊字符以在 CSV 文件中使用

collections - Backbone.js - "Syncing"集合之间的模型

javascript - 我可以使用主干应用程序作为另一个应用程序的组件吗?

javascript - 我应该如何检查 Jasmine 或javascript中的排序语句

javascript - 在 JavaScript 中的函数调用之间留出一些空间