简体   繁体   English

如何使用Backbone.js处理嵌套视图?

[英]How to handle nested views with Backbone.js?

In my app i have a few tagList , each one contains a few tags grouped by index_name . 在我的应用程序中,我有一些tagList ,每个列表包含按index_name分组的一些tags

I'm lost on how should i handle that with Backbone views. 我不知道该如何使用Backbone视图处理该问题。

I have a TagListView that extends Backbone.View , i guess i'll handle all events with this view. 我有一个扩展Backbone.ViewTagListView ,我想我会用这个视图处理所有事件。

My main question i : should i create a TagView with a render function that would be created & rendered for each tag in the TagListView render function ? 我的主要问题是:我是否应该使用TagView渲染函数中的每个标签创建和渲染的渲染函数创建TagListView


Here is what i do so far in my view : (is this ok for initialization ?!) 在我看来,这是我到目前为止所做的事情:(初始化可以吗?!)

    <ul id="strategy-tags">
        <!-- initial data -->
        <script type="text/javascript">
            AppData.strategyTagList = $.parseJSON('<?php echo json_encode($strategyTagList); ?>');
            strategyTagListView = new App.TagListView({el : "#strategy-tags", data: AppData.strategyTagList});
        </script>
    </ul>

Inside my core.js : 在我的core.js

    App.TagListView = Backbone.View.extend({

        // dom event specific to this item.
        events: {
        },

        initialize: function(options) {
            this.el = options.el;
        },

        render: function() {
            // let's construct the tag list from input data
            _.each(options.data, function(index) {
                // render index? <-- how ?
                _.each(index, function(tag) {
                    // render tag? <-- how ?
                    console.log(tag);
                });
            });
            return this;
        }

    });

Thanks a lot. 非常感谢。

I would say yes, the benefit of the individual 'item' view being able to re-render individually is that if you make changes to the model behind such an item, only that item will need to be re-rendered by the browser. 我会说是的,能够单独重新渲染单个“项目”视图的好处是,如果您对此类项目背后的模型进行更改,则只有该项目将需要由浏览器重新渲染。 Which is best for performance. 哪个是最佳性能。

It seems to be a question of granularity here, and one I've asked myself on occasion. 这里似乎是一个粒度问题,我有时会问自己一个问题。 My advice would be not to over do the views. 我的建议是不要过分看待观点。 It is akin to creating objects for everything in java - sometimes a simple string will suffice. 这类似于在Java中为所有对象创建对象-有时一个简单的字符串就足够了。 If you find a case of increased granularity in the future you can always come back and change it. 如果将来发现粒度增加的情况,可以随时回来进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM