2

バックボーン プロジェクトでこのプラグインを使用した経験のある人がいるかどうか疑問に思っています。

すべてのスクリプト テンプレート タグを 1 つのインデックス ファイルに格納する代わりに、テンプレートを必要とするビューと同じディレクトリに配置したいと考えました。

したがって、ノード オプションを使用してローカル テンプレートを要求し、それにレンダリングしてから、インデックス ファイルの #id に追加できることを期待していました (後で整理します)。

したがって、基本的には、バックボーン ビューである index.coffee と一緒に、ハンドルバー テンプレート (template.hbs) とそのコンパイル済み js (template.js) があります。

public
   |_ coffee
      |_views
        |_card
           |_list
              index.coffee
              template.hbs
              template.js

参考までに、私のうなり声ファイルは次のようになります。

handlebars: {
      compile: {
        options: {
          namespace: 'MyApp.Templates',
          node: true
        },
        files: {
          "public/coffee/views/card/list/template.js": "public/coffee/views/card/list/template.hbs"
        }
      }
    },

私のバックボーン ビュー (index.coffee) では、ハンドルバー テンプレートを次のように要求することを望んでいました。

class CardList extends Backbone.View
    template: require('./template')


    … 
    do some other shiz
    …


render: =>
    template = Handlebars.compile($(this.template).html())
    html = template
        model: this.model
    $(this.el).html(html)

これをレンダリングすると、インスペクターで次のエラーが吐き出されます。

Uncaught [object Object]

> template = handlebars.compile($(this.template).html());

私は明らかに自分が何をしているのかわからないので、このプラグインを適切に使用する方法について誰かが光を当ててくれることを願っています.

grunt-contrib-handlebars v0.3.5 を使用しています

どんな助けでも大歓迎です。

ありがとう

4

2 に答える 2

3

files オブジェクトを動的に構築することで、これを実現できる場合があります。

cwdグロビングパターンをサポートしているかどうかはわかりませんが、おそらくこのようなものです。destが相対的かどうかもわかりませんcwd。そうでない場合、これは機能しませんが、試してみる価値はあります。

handlebars: {
  dist: {
    options: {
      namespace: 'MyApp.Templates',
      node: true
    },
    // Glob for a directory of files, builds the files object, then map each
    // one to a new destination file.
    expand: true,
    cwd: './public/coffee/views/*',
    src: '**/*.hbs',
    dest: './',
    ext: '.js'
  }
},
于 2013-02-23T19:57:28.477 に答える
2

含めている template.js ファイルの中を見てください。grunt-comtrib-handlbars はそれを Javascript 関数にプリコンパイルする必要があるため、Handlebars.compile を呼び出す必要はもうありません。その template = Handlebars.compile 行を削除するだけです。

于 2013-02-23T16:04:57.660 に答える