当我点击第一页的'取消' - 没关系。但是当我移动到'第二页'时,'取消'不起作用。在这种情况下,路由器将应用重定向到路由。 我需要防止此重定向。如何防止Backbone.js中的路由?
“取消” - 隐藏链接,“第二页” - 移动到第二页
<script type="text/html" id="template">
<a href="#" class="cancel">Cancel</a>
<a href="#second">Second page</a>
</script>
<div id="container"></div>
<script>
var View = Backbone.View.extend({
template: $('#template').html(),
events: {
"click .cancel": "cancel"
},
cancel: function() {
this.$el.hide();
},
render: function() {
this.$el.html(this.template);
return this;
}
});
var AppRouter = Backbone.Router.extend({
routes: {
"": "first",
"second": "second"
},
创建视图并更换容器的HTML
links: function() {
var view = new View;
$('#container').html(view.render().el);
},
second: function() {
this.links();
$('#container').prepend("<h1>Second page</h1>");
},
first: function() {
this.links();
$('#container').prepend("<h1>First page</h1>");
}
});
$(document).ready(function() {
app = new AppRouter;
Backbone.history.start();
});
</script>
只是e.preventDefault() – rcarvalho