Search code examples
javascriptbackbone.jsbackbone-views

Call a new View from another View in Backbone


I'm a complete newbie to JS and to the Mongo/Mongoose/Express/Node/Backbone/Require stack... so there is a lot to learn and It's entirely possible I missed something here. However I am trying to store the result of a submitted form then move the user onto a different view if the save action was successful, so my View looks something like this:

define(['CoreView', 'text!templates/profile.html', 'models/Account'],

function(CoreView, profileTemplate, Account) {
    var profileView = CoreView.extend({
        el:         $('#content'),

        events:     {'submit form': 'update'},

        template:   _.template(profileTemplate),

        update:     function() {
                        /* 
                         * Gather the form data... then:
                         */
                        $.post('/accounts/' + this.model.get('_id'), 
                            {data: formData},
                            function(response) {
                                if (response == 'OK') {
                                    $.get('/#index');  // NO CHANGE??? 
                                    } else {
                                    console.err('Save Failed :(');
                                    }
                                });
                            return false;
                            },


        render:         function() {
                            if (this.model.get('_id') !== 'me') {
                                this.$el.html(this.template(this.model.toJSON()));
                                }
                            return this;
                            }
        });

return profileView;
});

Now it may well be I've missed out some mechanism in the routing where I can specify this sort of thing, any advice on why this is not working or a better solution is welcome.


Solution

  • $.get() issues a GET request for a page - it doesn't do anything to "move the user on to a different view". To do this you could use:

    document.location.href = /#index`
    

    Or a better solution if you have Backbone.Router or similar:

    router.navigate('index')