如何在HTML Page backbone&中直接使用_.template方法下划线

时间:2016-04-03 18:32:34

标签: javascript jquery html backbone.js underscore.js

我有一个mainView.js和mainView.html。

所有html都放在mainView.html中,而html中的编码也在不断增加。我想的是将html代码拆分成不同的html文件。

我直接在mainView.html中为我的子模板编写了_.template方法。但它没有用。

这就是我的尝试:

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;
});

1 个答案:

答案 0 :(得分:-1)

您应该要求&#39; one.html&#39;&#39; two.html&#39;,&#39; three.html&#39;在MainView.js中。将它传递给模板,然后使用它。否则模板不知道one.html和all。

代码看起来应该是这样的 MainView.js

   <% if session[:contacts_available]? %> 
       <p> Donec interdum turpis eget leo lobortis, sit amet lacinia ante vulputate. Maecenas hendrerit 
         euismod nulla in semper. Donec arcu nibh, faucibus at posuere id, dapibus non tellus. </p>

   <% else %>  
       <p> You're logged in as : <%= current_user.email %> <%= link_to "Log Out", logout_path %> </p>
       <p> Welcome to our service. You currently don't have any contact details under your username. 
       Please fill the below form to show the first contact detail of yours.  </p>

   <% end %>    

返回mainViewIn; });

MainView.html

  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);
    }
});

请原谅模板中的语法错误(如果有)。