Search code examples
javascripttemplatesbackbone.jsbackbone-viewsunderscore.js-templating

Template with Backbone and doubts about performance


I am experiencing some problems with Backbone/Underscore template because I'm really new to web developing.
I read this article: https://www.codementor.io/reactjs/tutorial/reactjs-vs-angular-js-performance-comparison-knockout and I found it really interesting so I was trying to write the same program but in backbone.

I would like to insert the list inside the div with the id=col-12 but it seems that backbone doesn't do that. I am sorry for my mistakes but I never programmed in Javascript or Html before and I'm sure I have some confusion in my mind that you can help me understand. This 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({
                tagName : 'div',
                el : $('#col-12'),

                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();

            date = new Date();

            // 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 });
            $(document.body).append(itemsView.render().el);

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


            });

            });

<div class="col-md-3">
    <div class="row">
        <div class="col-md-7">
            <h3>Performance Backbone:</h3>
        </div>

        <div class="col-md-5 text-right time" id="run-backbone">Run</div>
    </div>

    <div id="backbone">
        <div class="row">
            <div class="col-md-12 test-data" id="col-12">

            <!--HERE!!!!-->

            </div>
        </div>
    </div>
</div>


Solution

  • I resolved removing this line: el : $('#col-12'), and modifying this line: $(document.body).append(itemsView.render().el); to $("#col-12").append(itemsView.render().el);