2013-07-01 100 views
3

我有了主心骨/下划线应用程序通过require.js运行underscore.js - 对象不支持此属性或方法 - IE8

我刚才一直在做一些跨浏览器上我的应用程序检查,并认识到我的应用程序在加载ie8中的模板时遇到问题。

我的应用程序中,当渲染功能运行加载和在正确的模板调用

然后我用_.template转换代码视图部分内。

所以内作出我把这种

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

数据是在<%=%>格式和templateVars的HTML是renderTemplate其返回的数据(通常JSON结果)对象

的列表函数看起来像

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

的问题是,在IE8我得到一个错误是

对象不支持此属性或方法

这是它从该代码

_.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 /铬用来转换示数列于

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

线传回模板

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

Und erscore.js 1.3.2

能正常工作在IE9,FF和铬

谁能帮助?

+0

是否设置了文档类型? – inf3rno

+0

其设置像 - <!DOCTYPE html> - 是好的 – Dan

+0

它可以导致javascript问题,因为ie8不知道html5。我不知道这个设置会选择哪种文档模式...在MSIE运行javascript代码高度依赖于当前文档模式...我需要进一步调查,可悲的是我现在无法测试MSIE8 ...: S – inf3rno

回答

3

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

this question

你检查它是否工作与console.log()行注释掉,或者在开发工具是开放的?开发人员检查开发工具的好处是,你将能够看到错误发生在哪一行(如果它完全失败,因为console.log()将可用)。

+0

的model.toJSON()的一个例子嗨 - 它不是console.log - 我有这些多个,我也有这个代码.. if($。browser.msie){ (!window.console)window.console = {}; 012!(!window.console.log)window.console.log = function(){ } – Dan

+0

如果console.log()函数工作正常,您可以通过Dev Tools中的脚本调试告诉我们哪个行抛出异常吗?如发现哪个对象抛出“不支持该属性或方法”错误。 – dcarson

+0

其编译失败(vars); – Dan

相关问题