每次我按下按钮时,它都会添加我的数据并重新加载页面,然后列表就会消失。preventdefault 有效,但是每次我单击该按钮时,它都会不断添加到列表中。
如何清除列表并在每次单击按钮时重新开始?
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);
}
}
});