2

私は、通常は understand.js では問題にならない変数の使用に問題がありますが、JST と underscore.js を組み合わせると、どうやら苦労しているようです。

var something= SD.defaultView.extend({
    el: 'page',
    template: JST['app/www/js/templates/sex.ejs'],
    data: {
        header: 'some information!!!',
        image: '/img/path.jpg'
    },
    render: function () {
        var compiled = _.template(this.template(), this.data); //I pass in the complied JST template
        this.$el.html(compiled);
    }
});

レンダリングされた JST ファイル

this["JST"]["app/www/js/templates/sex.ejs"] = function (obj) {
    obj || (obj = {});
    var __t, __p = '', __e = _.escape;
    with (obj) {
        __p += ((__t = ( header )) == null ? '' : __t) + '<sexform>Hello There</sexform>';
    }
    return __p
};

エラー

ReferenceError: header is not defined - templates.js (line 21)

...obj = {});var __t, __p = '', __e = _.escape;with (obj) {__p +=((__t = ( header )...

セックス.ejs

<%= header %><sexform>Hello There</sexform>

背景情報

予想どおり、headerリーダーの時点では利用できません。これは、JST テンプレートが変更されるたびに grunt ファイルを介して発生しています。JST を間違った方法で実装しなければならないと感じています。

しかし、私にはこれがすべてを行う正しい方法のように思えます。

もちろん、sex.ejs 内でアンダースコア付きの変数を使用しようとしています。

このコードはすべてここで見ることができます: http://m.sexdiaries.co.uk/#wank 注: URL と同じように誤解を招く可能性がありますが、実際にはそうではありません。アダルト素材、教育アプリです。

4

1 に答える 1

6

ビューのテンプレートを定義するには、次のようにします。

template: JST['app/www/js/templates/sex.ejs'],

JST関数が含まれています (これは多かれ少なかれ、JST スタイルのプリコンパイル済みテンプレートを使用することの要点です) :

this["JST"]["app/www/js/templates/sex.ejs"] = function (obj) {

次に、これを行います。

var compiled = _.template(this.template(), this.data);
// function call ----------------------^^

2 つの点が間違っています。

  1. _.templateテンプレートをコンパイルするために既に呼び出しています。
  2. this.templateフィードされることを期待するコンパイル済みのテンプレート関数ですthis.data

修正は非常に簡単です。

var compiled = this.template(this.data);
于 2013-10-17T01:26:39.083 に答える