Backbone.js: how to redirect from one view to another view?

前端 未结 2 943
Happy的楠姐
Happy的楠姐 2021-01-31 10:13

In Backbone.js, what is the equivalent of HttpResponseRedirect in Django?

In one Backbone.View, I need to submit data to the server, and then redirect the user to anoth

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 10:52

    Here's the approach I've been taking:

    Assuming you have a router defined with routes like these:

    var MyRouter = Backbone.Router.extend({
        routes: {
            'first-route': 'first',
            'second-route': 'second'
        },
        first: function() {
            // render first view
        },
        second: function() {
            // render second view
        }
        // ...
    });
    

    I usually just use tags and set the href attribute to the hash I want to redirect to:

    First
    

    Alternatively, if you want to "redirect" from within your JavaScript, you could set the location hash explicitly:

    window.location.hash = 'first';
    

    I'm not 100% sure that the latter is 'best practice', but I've seen it done in a number of good tutorials.

    // Edit

    So after re-reading your original question, you could 'redirect' after sync calls back from the server simply by setting the location hash.

提交回复
热议问题