我有一个通过 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。
您是否检查过它是否适用于注释掉的 console.log()
行,或者在开发工具打开时?打开 Dev Tools 检查机智的好处是,您将能够看到错误发生在哪一行(如果它完全失败,因为 console.log()
将可用)。
关于json - underscore.js - 对象不支持此属性或方法 - ie8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17397574/