Search code examples
javascriptbackbone.jsgalleria

Galleria.io and backbone.js CPU usage 100%


I am having a weird issue with the image gallery Galleria.js and backbone.

This view is sometimes hidden and then re-rendered with a different model. What is happening is that after re-rendering 2 or 3 times, the browser's CPU usage spikes to 100%. I confirmed that Galleria is cause this, because I removed it from the view and the CPU was normal.

I am thinking that I might need to destroy the view when hiding or something? Not entirely sure about how to approach this.

App.HouseDetailView = Backbone.View.extend({
    el: '.house-details-area', 
    initialize: function() {
        this.template = _.template($('#house-details-template').html());
        App.Events.on('show_house', this.render, this);
        App.Events.on('show_main_view', this.hide, this);
    },
    events: {
        'click .btn-close': 'hide',
        'shown a[data-toggle="tab"][href=".detail-map"]' : 'show_map',
        'shown a[data-toggle="tab"][href=".detail-street-view"]' : 'show_street_view',
        'change .calculate-price': 'calculate_price',
    },
    render: function(model) {
          this.model = model;
          var html = this.template({model:model.toJSON()});
          $(this.el).html(html);
          Galleria.loadTheme('/static/js/libs/galleria.classic.min.js');
          Galleria.run('#galleria', {wait: true});
          $(this.el).show();
          return this;

    },
    hide: function() {
        $(this.el).hide();
        App.detailsRouter.navigate('/', true);
    },
    show_map: function() {
        // check if map already rendered
        if (this.$('.detail-map').html() === '') {
          var map = new App.DetailMapView({model:this.model});
          this.$('.detail-map').html(map.el);
          map.refresh();  
        }         
    },
    show_street_view: function() {
        if (this.$('.detail-street-view').html() === '') {
            var street_view = new App.DetailStreetView({model:this.model});
            this.$('.detail-street-view').append(street_view.el);
            street_view.render();
        }   
    },
    calculate_price: function (e) {
        var price_element = this.$('.price');
        var total_price = parseFloat(price_element.attr('data-total-price'));
        var people = parseFloat(price_element.attr('data-people'));
        // if selected is 1st option: Total price
        if (e.srcElement.selectedIndex === 0) {
            // show total price
            price_element.html('$' + total_price.toFixed(2));
        } else {
            // show per person price
            price_element.html('$' + (total_price/people).toFixed(2));
        }
    },
});

Solution

  • I was able to fix this issue by removing Galleria.loadTheme('/static/js/libs/galleria.classic.min.js'); from the view's render() method.

    I only load the Galleria theme once when the App is initialized.