我的骨幹應用目前其模板內嵌HTML如下:骨幹內嵌HTML模板與外部模板
<body>
<div id="container">
<header></header>
<nav></nav>
<div id="pagecontent"></div>
<footer></footer>
</div>
<!-- Featured Articles/Homepage Template -->
<script type="text/template" id="featuredarticles">
<link rel="stylesheet" href="css/homepagecontent.css" />
<section id="banner"></section>
<aside>
<div></div>
<div></div>
</aside>
<section id="main"></section>
<section id="opinion">
<div></div>
<div></div>
<div></div>
</section>
<section id="travel">
<div></div>
<div></div>
<div></div>
</section>
</script>
<!-- Content Articles Template -->
<script type="text/template" id="contentarticles">
<link rel="stylesheet" href="css/categorypagecontent.css" />
<section id="main"></section>
<aside></aside>
</script>
<!-- Require.js reference -->
<script src="/js/libs/require.js" data-main="/js/app.js"></script>
</body>
能I /我應該外部化HTML來代替。如果是這樣,我怎麼會外化這個(即使用View),所以它是如下:
<!-- Featured Articles/Homepage Template -->
<script type="text/template" id="featuredarticles">
<!-- HTML rendered externally -->
</script>
<!-- Content Articles Template -->
<script type="text/template" id="contentarticles">
<!-- HTML rendered externally -->
</script>
下面是我如何使目前從視圖模板的片段:
define(['underscore', 'backbone', 'collections/bannerCollection', 'collections/featuredArticlesCollection', ], function (_, Backbone, bannerCollection, featuredArticlesCollection) {
var featuredArticlesView = Backbone.View.extend({
el: $('#pagecontent'),
initialize: function() {
this.render();
},
render: function() {
var that = this;
var template = _.template($('#featuredarticles').html(), {});
that.$el.html(template);
return that;
}
});
return featuredArticlesView;
});
我一直在閱讀部分內容,但需要一些關於最佳實踐的指導,以及內聯HTML應該如何/如何分解。
你已經採取了看看requirejs 「文本」 模塊? https://github.com/requirejs/text – McGarnagle 2014-10-10 20:46:42
我剛開始考慮這一點。我會仔細看看。 – Kode 2014-10-10 20:51:04
您也可以將您的Underscore模板預編譯爲JavaScript服務器端的位。 't = _.template(x)'在't'中給你留下了一個JavaScript函數,並且源代碼可以在't.source'中找到。順便說一下,'_.template'的形式是[不再可用,你必須立即編譯和調用。](http://stackoverflow.com/a/25881231/4798630) – 2014-10-10 20:54:31