我必須在html中插入一個硬編碼的簡單菜單,但我不知道應該在哪裏插入它。 我應該直接在路由器中添加html代碼嗎?如何?在主幹中插入靜態菜單
1
A
回答
0
你應該使用一個骨幹視圖,並讓路由器使其:
http://jsfiddle.net/rd13/g3U7j/11/
var MyView = Backbone.View.extend({
el: '.container',
template: "<menu><li>Item</li></menu>",
render: function() {
this.el.innerHTML = this.template;
return this;
}
});
var r = Backbone.Router.extend({
routes: {
'': 'default'
},
default: function() {
new MyView().render();
}
});
var router = new r();
Backbone.history.start();
2
不,你不應該使用一個路由器,你應該做一個Backbone.View對象這就是應該創建HTML並添加它的那種對象。
沒有模板
var view = Backbone.View.extend({
.
. other backbone stuff
.
,menu: '<div> menu </div>'
,render: function(){
var compiledHTML= $(this.menu);
$('selector').append(compiledHTML);
}
});
使用模板
使用HTML菜單在您的網頁HTML插入爲使用
型的簡單方法的簡單之路
。 。你的html代碼 。
.
. end of your html code
.
<script type="text/template" id="marker_small_info_template">
<div> xxx </div>
</script>
</body>
,然後在骨幹使用jQuery把它包起來,並在需要的地方添加到您的網頁。
var view = Backbone.View.extend({
,render: function(){
var compiledHTML= _.template($("#marker_small_info_template").html());
$('selector').append(compiledHTML);
return this;
}
});
的複雜和花哨的方式做到這一點(require.js +模板)
是要有HTML代碼作爲一個單獨的文件的模板(Underscore.template例如)然後在Backbone.View中使用Require.JS將其「編譯」爲我獲取,並使用JQuery來包裝和添加它。
define([
'text!templates/menuFrenteItem.html'
],
function (templateMenuItem) {
return Backbone.View.extend({
.
.
.
,smallInfo: function(variables){ return _.template(templateMenuItem, variables)}
,render: function(){
var compiledHTML = $(this.smallInfo(dataToCompileTemplate));
$('selector').append(compiledHTML);
return this;
}
}
我認爲這是學習在JavaScript中使用模板,這個工具添加到您的安全帶的好機會。
相關問題
- 1. 動態和靜態的MenuItems,無論是在主菜單,文本菜單
- 2. 靜態HTML菜單
- 3. Wordpress插入主菜單中的自定義菜單
- 4. Jquery靜態菜單到Wordpress菜單
- 5. 創建靜態菜單CSS
- 6. 靜態下拉菜單
- 7. ActionBarSherlock靜態附件菜單
- 8. 靜態類別菜單
- 9. 如何在WordPress中插入TwentyThirteen主題的二級菜單?
- 10. 在Connect IQ Simulator中輸入主菜單
- 11. 靜態jquery HTML動態菜單的菜單
- 12. Jqgrid中的靜態下拉菜單編輯彈出菜單
- 13. 簡單的靜態html菜單
- 14. 在html菜單中插入圖片
- 15. 靜態主類靜態類?
- 16. OctoberCMS後臺主菜單中有不同插件的子菜單
- 17. 在單獨的主幹類文件中映射主幹事件
- 18. 如何在Java中的靜態方法中插入非靜態變量?
- 19. 在路由器主幹中插入定時器功能
- 20. 靜態變量干擾ASP.net?
- 21. 插入div jQuery的菜單
- 22. 實現一個靜態菜單
- 23. 創建一個靜態菜單欄
- 24. CakePHP:高效加載靜態左菜單
- 25. 靜態頭+菜單,滾動體
- 26. HTML Div使用靜態菜單定位
- 27. Backbone.js - 幫助靜態菜單的方法
- 28. WordPress的靜態菜單(WordPress的3.8)
- 29. 靜態下拉菜單使用AJAX,PHP
- 30. Android:總是顯示菜單欄/靜態菜單欄
你能否簡單地解釋一下你想要達到的目標?靜態菜單意味着什麼?爲什麼這麼特別讓你如此困惑,在哪裏插入它? –