每當我按下按鈕,它會添加我的數據並重新加載頁面,列表消失。預防默認工作,但每次我點擊它不斷添加到列表中的按鈕。每次按下按鈕清除視圖並重新開始 - backbone.js
如何清除列表並重新開始每個按鈕點擊?每次單擊該按鈕時
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.7.2.js"></script>
<script type="text/javascript" src="Scripts/underscore.js"></script>
<script type="text/javascript" src="Scripts/backbone.js"></script>
</head>
<body>
<div>
<div id="container">
<button>Load</button>
<ul id="list"></ul>
</div>
<div id="list-template">
<li><a href=""></a></li>
</div>
<script type="text/javascript" src="JavaScript.js"></script>
</div>
</body>
</html>
的JavaScript
model = new Backbone.Model({
data: [
{ text: "site 1", href: "link1.htm" },
{ text: "site 2", href: "link2.htm" },
{ text: "site 3", href: "link3.htm" },
{ text: "site 4", href: "link4.htm" },
{ text: "site 4", href: "link4.htm" },
{ text: "site 4", href: "link4.htm" },
{ text: "site 4", href: "link4.htm" },
]
});
var View = Backbone.View.extend({
initialize: function() {
//console.log('initializing ' + this.options.blankOption);
this.template = $('#list-template').children();
},
el: '#container',
events: {
'click button' : 'render'
},
//model:model,
render: function(event){
//event.preventDefault();
var data = this.model.get('data');
for (var i = 0; i < data.length; i++) {
var li = this.template.clone().find('a').attr('href', data[i].href).text(data[i].text).end();
this.$el.find('ul').append(li);
}
}
});
請張貼HTML太.. – PSR 2013-02-25 10:05:15
非常感謝 – 2013-02-25 10:09:18