2012-04-10 11 views
1

私はRails 3.2.3プロジェクトでBackbone.jsを取得していますが、動作していますが、これをクリーンアップして別のJSTファイルに入れたいと思います。Backbone.js Rails 3.2のテンプレートファイル

私が最初その後、私は「show.jst」と呼ばれるそこにファイルを作成し

<project>/app/assets/templates/appointments 

私のテンプレートを保持するディレクトリを作成しました。私は、私が一緒に行って、外部テンプレートファイルを使用するには、次のコードを変換しようとしたので、私はRailsの3.2.xの下Jammitをインストールする必要はありません何を読んでから、

:ここ

window.AppointmentView = Backbone.View.extend({ 
    template: _.template('<h3><%= topic %></h3>'), 

    render: function(){ 
     var attributes = this.model.toJSON(); 
     this.$el.html(this.template(attributes)); 
     return this; 
    } 
}); 

をされますこれまでの私の試み:

window.AppointmentView = Backbone.View.extend({ 
    render: function(){ 
     var attributes = this.model.toJSON(); 
     var html = JST['appointments/show'](attributes); 
     this.$el.html(html); 
     return this; 
    } 
}); 

私は次のことを持っている私のshow.jstファイル内部:

<h3><%= topic %></h3> 

(トピックは私の「予定」モデル内のフィールドです)

これは画面に何も表示されず、画面に何も印刷されません。

これを修正して外部テンプレートファイルを使用するにはどうすればよいですか?

更新

私は他のすべてがそうのような文を必要とする前に、私は(私がテストしてる場所に応じて)required_tree ./templatesまたは../templatesを持っていることを確認作りました:

私は

アプリ/資産/ JavaScriptの/テンプレート/予定を次のように私のshow.jstテンプレートファイルを入れて試してみた

#= require jquery 
#= require jquery_ujs 
#= require backbone-rails 
#= require_tree ../templates 
#= require_tree ./models 
#= require_tree ./collections 
#= require_tree ./views 
#= require_tree ./routers 

/show.jst

app/assets/templates/appointments/show.jst

ファイルshow.jst.ejsに名前を付けて、gemfileにgem 'ejs'を含めてみました。

いずれもテンプレートをロードしていません。私はそれは次のように私の道にあったことを確認したアプリ/資産の下に私のテンプレートを保存する場合:

config.assets.paths << "#{ Rails.root }/app/assets/templates" 

これは、どちらかの助けにはならなかったが、それはスプロケットエラーを取り除くでした。

私はまだテンプレートjs​​tファイルを読み込めませんでした。私が読んだものはすべて別の方法を示唆しています。私はこれらの多くを試しましたが、Rails 3.2.3と互換性がないかもしれません。

答えて

3

これはうまくいくことがわかりました。私のappointment_show.jsで、私はそうのように私のテンプレートを呼び出す:

window.AppointmentView = Backbone.View.extend({ 
    template: JST["appointments/show"], 

    render: function(){ 
     this.$el.html(this.template(this.model.toJSON())); 
     return this; 
    } 
}); 

私は自分のアプリケーションでは、テンプレートのディレクトリが含まれていることを確認してください。JS:に忘れてはいけない

AppointmentsBackboneJs::Application.configure do 
    config.assets.paths << "#{ Rails.root }/app/assets/templates" 
end 

を:

#= require jquery 
#= require jquery_ujs 
#= require backbone-rails 
#= require_tree ../templates 
#= require_tree ./models 
#= require_tree ./collections 
#= require_tree ./views 
#= require_tree ./routers 

はenvironment.rbに以内にあなたのパスにアプリ/資産/ templatesディレクトリを追加するには、Gemfile

gem 'backbone-rails' 
gem 'ejs' 

でこれらの宝石を含めますランバンドル&再起動サーバ