2016-04-03 17 views
-3

私はmainView.jsとmainView.htmlを持っています。HTMLページバックボーンとアンダースコアで_.templateメソッドを直接使用する方法

すべてのhtmlはmainView.htmlに置かれ、html内のコーディングは増加し続けます。私が考えたのは、htmlコードを別のhtmlファイルに分割することです。

と私のサブテンプレートの_.templateメソッドを私のmainView.htmlに直接書きました。それは動作していません。

これは私が試したものです:

MainView.html

あなたが必要なはずです
<div id="section"> 
<div id="one"><%_.template('one.html')%></div> 
<div id="two"><%_.template('two.html')%></div> 
<div id="three"><%_.template('three.html')%></div> 
</div> 

MainView.js

define(['jquery', 'underscore', 'backbone', 'text!mainView.html'], function($, _, Backbone, mainViewHTML) { 
    var mainViewIn = Backbone.View.extend({ 
    render : function(){ 
    var that = this; 
    var tmpl = _.template(mainViewHTML); 
    that.$el.html(tmpl); 
    } 
}); 
return mainViewIn; 
}); 
+2

* "は機能していません" *は意味のある問題文ではありません。変数 'mainViewHTML'はどこに定義されていますか?スローされたエラー? – charlietfl

+0

@charlitfl:ここに追加した定義メソッドがあります。 –

+0

そして、それは何かを推測することになっていますか? [mcve] – charlietfl

答えて

-1

'one.html'、 'two.html'、 ' three.html 'をMainView.jsに追加します。それをテンプレートに渡し、それを使用します。それ以外のテンプレートは、one.htmlとすべてのものを認識しません。

コードは、この MainView.js

define(['jquery', 'underscore', 'backbone', 'text!mainView.html', 'text!one.html', 'text!two.html', 'text!three.html'], function($, _, Backbone, mainViewHTML, one, two, three) { 
    var mainViewIn = Backbone.View.extend({ 
    render : function(){ 
    var that = this; 
    var template = mainViewHTML({one: one, two: two, three: three}); 
    var tmpl = _.template(mainViewHTML); 
    that.$el.html(tmpl); 
    } 
}); 

リターンmainViewInようになるはずです。 });いずれかが存在する場合

MainView.html

<div id="section"> 
<div id="one"><%_.template({one})%></div> 
<div id="two"><%_.template({two})%></div> 
<div id="three"><%_.template({three})%></div> 
</div> 

テンプレートの構文エラーはご容赦ください。

+0

これはなんですか? '{1}' ...?!これはjavascriptの構文エラーです。 –

+0

ハンドルバーを使用している場合は、テンプレートに渡されるデータを取得する方法です –

+0

コードがハンドルバーを使用していない、アンダースコアを使用しています –

関連する問題