私は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、およびクロムで正常に機能します
誰でも助けることができますか?