첫 페이지에서 '취소'를 클릭하면됩니다. 괜찮습니다. 그러나 이 '두 번째 페이지'로 이동하면 '취소'가 작동하지 않습니다. 이 경우 라우터는 응용 프로그램을 ""경로로 리디렉션합니다. 이 리디렉션을 방지해야합니다.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"
},
로 이동하고 난 당신이 취소 핸들러에서 false를 반환해야한다고 생각
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