3

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

我刚刚对我的应用程序进行了一些跨浏览器检查,并意识到我的应用程序在加载 ie8 的模板时遇到问题。

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

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

所以在渲染中我称之为

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

data 是 <%=%> 格式的 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();
}; 

下划线.js 1.3.2

这在 ie9、FF 和 chrome 中运行良好

谁能帮忙?

4

1 回答 1

3

很可能是因为console.log()IE8 不支持,除非您打开了开发工具。

看到这个问题

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

于 2013-07-01T04:49:33.367 回答