最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone showinghiding rendered views best practices - Stack Overflow

programmeradmin2浏览0评论

New to using Backbone and have a very simple application. Basically there are Clients and ClientItems. I have a view to show all Clients and if you click on a Client you get taken to their ClientItems. Going to this ClientItems view should just hide the Clients view and going back to Clients should hide ClientItems. Now, in my render() function for each view, it is going through the collections and dynamically adding stuff to the page. When I go back and forth between the two (using the back button) I don't really need to fully render again as all the data is there in the page, just hidden. Where should this logic go? Right now I have it in the render() function but it feels sloppy, what is the preferred way of handling this?

New to using Backbone and have a very simple application. Basically there are Clients and ClientItems. I have a view to show all Clients and if you click on a Client you get taken to their ClientItems. Going to this ClientItems view should just hide the Clients view and going back to Clients should hide ClientItems. Now, in my render() function for each view, it is going through the collections and dynamically adding stuff to the page. When I go back and forth between the two (using the back button) I don't really need to fully render again as all the data is there in the page, just hidden. Where should this logic go? Right now I have it in the render() function but it feels sloppy, what is the preferred way of handling this?

Share Improve this question asked Feb 22, 2012 at 16:53 BrandonBrandon 2,1254 gold badges25 silver badges33 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

We are using a global variable App with several common function used across application:

var App = {
    initialize : function() {

        App.views = {
            clientView : new ClientsView(),
            clientItemView : new ClientsItemsView()
        }
    },

    showView: function(view){
        if(App.views.current != undefined){
            $(App.views.current.el).hide();
        }
        App.views.current = view;
        $(App.views.current.el).show();
    },

    ...
}

And then I use this App from other parts of application:

App.showView(App.views.clientView);

IntoTheVoid's solution is good – it's nice to have a single place to hide/show views. But how do you activate the logic?

In my experience, routers are the best place for this. When a route changes and the appropriate function is called, you should update the active, visible view(s).

What if you need multiple views to be visible at once? If you have a primary view that always changes when the route changes, and multiple subsidiary sticky views, you need not worry. But if it's more complex than that, think of creating a ComboView that neatly packages all the relevant views into one containing el node. That way the above logic still works, and your router functions are not littered with logic for managing what views are visible at the moment.

发布评论

评论列表(0)

  1. 暂无评论