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、およびクロムで正常に機能します

誰でも助けることができますか?

4

1 に答える 1

3

ほとんどのconsole.log()場合、開発ツールを開いていない限り、IE8 ではサポートされていないためです。

この質問を参照してください

console.log()行をコメントアウトした状態で動作するか、Dev Tools を開いた状態で動作するかを確認しましたか? 開発ツールを開いてチェックする利点は、エラーが発生している行を確認できることです (console.log()利用可能になるためにエラーが発生した場合)。

于 2013-07-01T04:49:33.367 に答える