Merge branch 'hotfix/21.56.9' into master
[gitter.git] / public / js / views / loading-mixin.js
blobc85014359ec90b739c368ea28123f72e95ce1617
1 'use strict';
3 var Marionette = require('backbone.marionette');
4 var Backbone = require('backbone');
5 var loadingTemplate = require('./tmpl/loading.hbs');
7 var LoadingView = Marionette.ItemView.extend({
8   template: loadingTemplate
9 });
11 // TODO: move this to a behavior
12 // Mixin for Marionette.CollectionView classes
13 module.exports = {
14   loadingView: LoadingView,
15   initialize: function() {
16     this.showEmptyView = this.showLoadingView;
17   },
18   showLoadingView: function() {
19     if (this.collection.loading) {
20       var LoadingView = Marionette.getOption(this, 'loadingView');
22       if (!this.loadingModel) {
23         this.loadingModel = new Backbone.Model();
24       }
26       var v = this.children.findByModel(this.loadingModel);
28       if (LoadingView && !v) {
29         this.addChild(this.loadingModel, LoadingView, 0);
30         this.listenToOnce(this.collection, 'loaded', function() {
31           this.removeChildView(this.loadingModel);
33           if (this.collection.length === 0) {
34             this.constructor.prototype.showEmptyView.call(this);
35             return true;
36           }
37         });
38       }
39       return true;
40     }
42     this.constructor.prototype.showEmptyView.call(this);
43     return true;
44   }