javascript - Backbone nested views - Stack Overflow

I'm working on a Backbone application and I'm not sure if the way what I'm trying to do

I'm working on a Backbone application and I'm not sure if the way what I'm trying to do is the correct way.

I have an application view and inside that application view I'm trying to append a collection view, and each view in that collection is a collection too.

Let me explain that graphically.

----------------------------------------------------------------------
|                                                                    |
|    Application view                                                |
|                                                                    |
|    -------------------------------------------------------------   |
|    |  Windows Collection view                                  |   |
|    |                                                           |   |
|    |  --------------------------   --------------------------  |   |
|    |  | Tabs collection view   |   | Tabs collection view   |  |   |
|    |  |                        |   |                        |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  | |Tab view| |Tab view|  |   | |Tab view| |Tab view|  |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  |                        |   |                        |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  | |Tab view| |Tab view|  |   | |Tab view| |Tab view|  |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  |------------------------|   |------------------------|  |   |
|    |                                                           |   |
|    |  --------------------------   --------------------------  |   |
|    |  | Tabs collection view   |   | Tabs collection view   |  |   |
|    |  |                        |   |                        |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  | |Tab view| |Tab view|  |   | |Tab view| |Tab view|  |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  |                        |   |                        |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  | |Tab view| |Tab view|  |   | |Tab view| |Tab view|  |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  |------------------------|   |------------------------|  |   |
|    |                                                           |   |
|    -------------------------------------------------------------   |
|                                                                    |
|                                                                    |
----------------------------------------------------------------------

Currently I'm loading the application view from the initialize method in my Backbone router. That view loads the Windows collection view.

The main problem is that I'm not sure if I'm on the right way. The second problem is that I'm not really sure how to load each Tabs collection view from my Windows Collecion view.

PS: Just to make things even clearer, I'm trying to replicate Firefox's panorama view: .1d/i/tim//2010/08/24/firefox-panorama.jpg

I'm working on a Backbone application and I'm not sure if the way what I'm trying to do is the correct way.

I have an application view and inside that application view I'm trying to append a collection view, and each view in that collection is a collection too.

Let me explain that graphically.

----------------------------------------------------------------------
|                                                                    |
|    Application view                                                |
|                                                                    |
|    -------------------------------------------------------------   |
|    |  Windows Collection view                                  |   |
|    |                                                           |   |
|    |  --------------------------   --------------------------  |   |
|    |  | Tabs collection view   |   | Tabs collection view   |  |   |
|    |  |                        |   |                        |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  | |Tab view| |Tab view|  |   | |Tab view| |Tab view|  |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  |                        |   |                        |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  | |Tab view| |Tab view|  |   | |Tab view| |Tab view|  |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  |------------------------|   |------------------------|  |   |
|    |                                                           |   |
|    |  --------------------------   --------------------------  |   |
|    |  | Tabs collection view   |   | Tabs collection view   |  |   |
|    |  |                        |   |                        |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  | |Tab view| |Tab view|  |   | |Tab view| |Tab view|  |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  |                        |   |                        |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  | |Tab view| |Tab view|  |   | |Tab view| |Tab view|  |  |   |
|    |  | ---------- ----------  |   | ---------- ----------  |  |   |
|    |  |------------------------|   |------------------------|  |   |
|    |                                                           |   |
|    -------------------------------------------------------------   |
|                                                                    |
|                                                                    |
----------------------------------------------------------------------

Currently I'm loading the application view from the initialize method in my Backbone router. That view loads the Windows collection view.

The main problem is that I'm not sure if I'm on the right way. The second problem is that I'm not really sure how to load each Tabs collection view from my Windows Collecion view.

PS: Just to make things even clearer, I'm trying to replicate Firefox's panorama view: http://i.i../cnwk.1d/i/tim//2010/08/24/firefox-panorama.jpg

Share Improve this question edited May 19, 2013 at 18:20 alexandernst asked May 19, 2013 at 17:35 alexandernstalexandernst 15.1k25 gold badges107 silver badges213 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I would highly remend using Marionette.js to structure your application.

It already has collection views built in which makes rendering easy. Your application seems to be a perfect use case. You will get a lot of boilerplate code for free.

I'm just posting this here so others can see how I solved the problem

A working demo of the solution can be found here (original fiddle).

As you can see from the link, the work is done thanks to Marionette's CompositeView which lets recursively render collections.

var TreeView = Backbone.Marionette.CompositeView.extend({

    initialize: function(){
        if(this.model.tabs){
            this.template = "#window-template";
        }else{
            this.template = "#tab-template";
        }
        this.collection = this.model.tabs;
    },

    appendHtml: function(cv, iv){
        cv.$("#tabs").append(iv.el);
    },
    onRender: function() {
        if(_.isUndefined(this.collection)){
            this.$("#tabs").remove();
        }
    }
});

The small trick I'm using in the initialize (the if/else with the template asignation) works the following way:

I get the current model and check if it has a "tabs" key. If it does have it, it means that the current model is a Window Data Model, so I need to use the window-template, else use the tab-template

The rest is pretty much plain Backbone structure.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745663727a4638995.html

相关推荐

  • javascript - Backbone nested views - Stack Overflow

    I'm working on a Backbone application and I'm not sure if the way what I'm trying to do

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信