[英]PreventDefault event not working in Backbone view
I'm learning Backbone.js from lightweight django and i'm unable to get the below function from triggering a default event. 我正在从轻量级django中学习Backbone.js,并且无法从触发默认事件中获取以下功能。
the below is my html for the button 下面是我的按钮的HTML
<script type="text/html" id="header-template">
<span class="title">Scrum Board Example</span>
<% if (authenticated ) { %>
<nav>
<a href="/" class="button">Your Sprints</a>
<a href="#" class="logout">Logout</a>
</nav>
<% } %>
</script>
and this is my backbone view 这是我的骨干观点
var HeaderView = TemplateView.extend({
tagName: 'header',
templateName: '#header-template',
events: {
'click a.logout': 'logout'
},
getContext: function () {
return {authenticated: app.session.authenticated()};
},
logout: function (event) {
event.preventDefault();
console.log('clicked');
app.session.delete();
window.location = '/';
}
});
whenever I click on the a href button - it doesn't seem to be triggering the logout function in my view model. 每当我单击a href按钮时,它似乎都不会触发我的视图模型中的注销功能。
Am i going wrong somewhere else? 我在其他地方出错吗?
Thanks,. 谢谢,。 KJ
J
If you use preventDefault() there is no reason to use href. 如果使用preventDefault(),则没有理由使用href。
<a class="button">Your Sprints</a>
But if you use window.location = '/' why using preventDefault. 但是,如果使用window.location ='/',为什么要使用preventDefault。
Also using a callback for your delete function could be cool. 此外,将回调函数用于删除功能可能很酷。
var callback = window.location = '/';
app.session.delete(callback);
In app.session.delete: 在app.session.delete中:
app.session.delete = function(callback) {
// do stuff
if(callback) {
callback();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.