admin管理员组文章数量:1026989
When I click 'Cancel' from First page - it's ok. But 'Cancel' doesn't work when I move to 'Second page'. In this case router redirects application to "" route. I need to prevent this redirection.
"Cancel" - hide links, "Second page" - move to Second page
<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"
},
Create view and replace container's 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>
When I click 'Cancel' from First page - it's ok. But 'Cancel' doesn't work when I move to 'Second page'. In this case router redirects application to "" route. I need to prevent this redirection.
"Cancel" - hide links, "Second page" - move to Second page
<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"
},
Create view and replace container's 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>
Share
Improve this question
asked Jun 26, 2012 at 11:59
chessmanchessman
472 silver badges6 bronze badges
1
- just e.preventDefault() – rcarvalho Commented Jul 2, 2014 at 18:55
2 Answers
Reset to default 3I think you should return false from the cancel handler to prevent navigation to #
:
cancel: function (e) {
this.$el.hide();
return false;
},
http://api.jquery./on/ says:
Returning false from an event handler will automatically call event.stopPropagation() and event.preventDefault().
You can also solve blocking hash havigation in a reusable way like in this answer:
Prevent navigating to another view if contents are unsaved
This solution prevents Backbone router callbacks from firing, and though it can only run AFTER the hash has changed, this function sets the previous hash fragment back so it can't be noticed
Also, for this exeact problem, you can simply change from
<a href="#">...</a>
To
<a>...</a>
it will act as a normal HTML anchor tag (e.g. cursor is set to pointer
) but won't navigate anywhere
When I click 'Cancel' from First page - it's ok. But 'Cancel' doesn't work when I move to 'Second page'. In this case router redirects application to "" route. I need to prevent this redirection.
"Cancel" - hide links, "Second page" - move to Second page
<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"
},
Create view and replace container's 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>
When I click 'Cancel' from First page - it's ok. But 'Cancel' doesn't work when I move to 'Second page'. In this case router redirects application to "" route. I need to prevent this redirection.
"Cancel" - hide links, "Second page" - move to Second page
<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"
},
Create view and replace container's 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>
Share
Improve this question
asked Jun 26, 2012 at 11:59
chessmanchessman
472 silver badges6 bronze badges
1
- just e.preventDefault() – rcarvalho Commented Jul 2, 2014 at 18:55
2 Answers
Reset to default 3I think you should return false from the cancel handler to prevent navigation to #
:
cancel: function (e) {
this.$el.hide();
return false;
},
http://api.jquery./on/ says:
Returning false from an event handler will automatically call event.stopPropagation() and event.preventDefault().
You can also solve blocking hash havigation in a reusable way like in this answer:
Prevent navigating to another view if contents are unsaved
This solution prevents Backbone router callbacks from firing, and though it can only run AFTER the hash has changed, this function sets the previous hash fragment back so it can't be noticed
Also, for this exeact problem, you can simply change from
<a href="#">...</a>
To
<a>...</a>
it will act as a normal HTML anchor tag (e.g. cursor is set to pointer
) but won't navigate anywhere
本文标签: javascriptHow to prevent routing in BackbonejsStack Overflow
版权声明:本文标题:javascript - How to prevent routing in Backbone.js? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745663775a2162051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论