Cleaning views with backbone.js?

后端 未结 1 770
挽巷
挽巷 2020-12-15 13:56

I am working on a backbone.js-application and have reached the point where I have a number of routers and views representing each part of my application. In the simplified r

相关标签:
1条回答
  • 2020-12-15 14:28

    I have a really simple implementation, I'm just starting my application now and don't know how this is going to hold up in the long run, but it looks something like this:

    Edit: Here's what the entire file would look like. this.render will be another function of myRouter.

    var myRouter = Backbone.Router.extend({
        routes: {
            'path/to/account' : 'account',
            'path/to/users': 'users'
        }
    
        account: function() {
            view = new AccountView({});
            this.render(view);
        },
    
        users: function() {
            view = new UserView({});
            this.render(view);
        },
    
        render: function (view) {
            //Close the current view
            if (this.currentView) {
                this.currentView.remove();
            }
    
            //render the new view
            view.render();
    
            //Set the current view
            this.currentView = view;
    
            return this;
        }
    });
    
    0 讨论(0)
提交回复
热议问题