简体   繁体   English

有关在Backbone中呈现列表的更好逻辑/性能的建议

[英]Advices for better logic/performance on rendering a list in Backbone

I'm really new to web developing and Backbone. 我真的是Web开发和Backbone的新手。 I would like some advices for better performance on this program I have written. 我想要一些建议,以提高我编写的该程序的性能。 I tried to write a program similar to these: React vs AngularJS vs KnockoutJS: a Performance Comparison 我试图编写类似于以下程序: React vs AngularJS vs KnockoutJS:性能比较

In particular, I'd like to know if $( "#insideID" ).detach(); 特别是,我想知道是否$( "#insideID" ).detach(); is a good strategy or not. 是不是一个好策略。

Here is part of my code: 这是我的代码的一部分:

<script id="itemTemplate" type="text/template">
<%= name %>
</script>  

        $( document ).ready(function() {

            // Model
            var Item = Backbone.Model.extend({
            });

            // Collection
            var ItemsCollection = Backbone.Collection.extend({
                model: Item
            });


            // View for all elements
            var ItemsView = Backbone.View.extend({
                className: 'inside',
                id: 'insideID',

                render: function() {
                    this.collection.each(function(item) {
                        var itemView = new ItemView({ model: item });
                        this.$el.append(itemView.render().el);
                    }, this);

                    return this;
                }
            });

            // View for a single element
            var ItemView = Backbone.View.extend({
                tagName : 'span',

                template: _.template($('#itemTemplate').html() ),

                render: function() {
                    this.$el.html( this.template(this.model.toJSON()) );
                    return this;
                }
            });


            var runBackbone = document.getElementById("run-backbone");
            runBackbone.addEventListener("click", function() {

            var data = _buildData(); //It returns an array of strings

            date = new Date();

            $( "#insideID" ).detach();

            // It creates the collection
            var itemsCollection = new ItemsCollection;

            for (var i = 0; i < data.length; i++)
            {
                newItem = new Item ({name: data[i].label});
                itemsCollection.add(newItem);
            }

            var itemsView = new ItemsView({ collection: itemsCollection });
            $("#col-12").append(itemsView.render().el);

            runBackbone.innerHTML = (new Date() - date) + " ms";


            });

            });

In your itemsView render method, instead of appending each itemView to your element, causing many DOM reflows especially if you have many items in your collection, you could use a documentFragment as a container that you append only one time. 在您的itemsView呈现方法中,不是将每个itemView附加到元素上,而是导致很多DOM重排,尤其是如果您的集合中有很多项目,则可以使用documentFragment作为仅附加一次的容器。

render: function() {
    this.$el.empty();

    var container = document.createDocumentFragment();

    this.collection.each(function(item) {
        var itemView = new ItemView({ model: item });
        container.appendChild(itemView.render().el);
    }, this);

    this.$el.append(container);
    return this;
} 

You might want to check out Marionette framework. 您可能想查看Marionette框架。 It will help you if your application starts to get bigger and complex (eg grids, layouts, compositeViews etc), Backbone Marionette 如果您的应用程序开始变得越来越大和复杂(例如,网格,布局,compositeViews等), 主干木偶 ,它将为您提供帮助

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

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