Skip to content

Instantly share code, notes, and snippets.

@js62789
Created July 31, 2014 21:36
Show Gist options
  • Save js62789/d20248a73e7422df5fa6 to your computer and use it in GitHub Desktop.
Save js62789/d20248a73e7422df5fa6 to your computer and use it in GitHub Desktop.
A CompositeView which accepts a loadingView
var CompositeView = Marionette.CompositeView.extend({
_initialEvents: function(){
this._isLoading = true;
if (this.collection){
this.listenTo(this.collection, "add", this.addChildView);
this.listenTo(this.collection, "remove", this.removeItemView);
this.listenTo(this.collection, "reset", this.render);
this.listenTo(this.collection, "sync", function () {
this._isLoading = false;
});
}
},
isLoading: function () {
return this._isLoading;
},
getLoadingView: function () {
return Marionette.getOption(this, "loadingView");
},
showEmptyView: function(){
var model = new Backbone.Model();
var LoadingView = this.getLoadingView();
if (LoadingView && this.isLoading() && !this._showingLoadingView) {
this._showingLoadingView = true;
this.addItemView(model, LoadingView, 0);
} else {
var EmptyView = this.getEmptyView();
if (EmptyView && !this._showingEmptyView){
this._showingEmptyView = true;
this.addItemView(model, EmptyView, 0);
}
}
},
closeEmptyView: function(){
if (this._showingLoadingView){
this.closeChildren();
delete this._showingLoadingView;
} else if (this._showingEmptyView){
this.closeChildren();
delete this._showingEmptyView;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment