当我从首页单击“取消”时 - 没关系。但是当我移至“第二页”时,“取消”不起作用。在这种情况下,路由器将应用程序重定向到“”路由。我需要防止这种重定向。
“取消” - 隐藏链接,“第二页” - 移动到第二页
<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>