Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to separate the two cases for rendering the emptyView?

Tags:

marionette

Is there a way to separate the two cases for rendering the emptyView?
1. When the CollectionView is just created. The collection is still empty
2. After collection is fetched but the data is empty.

_CollectionView = Backbone.Marionette.CollectionView.extend({
    emptyView: _EmptyView,
    itemView: _ItemView,
    initialize: function () {
        this.collection =  new Backbone.Collection ();
        this.collection.fetch();
    },//initialize

});
like image 460
Ming Chan Avatar asked Feb 02 '26 12:02

Ming Chan


1 Answers

this is the way I have done this in the past. Set your 'emptyView' to be your loading view and then after the collection has synced, set the 'emptyView' to your actual EmptyView if required. I have also used this in 'onBeforeRender' as in the example below you may need to re-render your view if it has already been rendered with the 'EmptyView':

emptyView: LoadingView,
collectionEvents: {
    'sync': 'onSync'
},
onSync: function () {
    if(this.collection.length === 0) {
        this.emptyView = EmptyView;
        //may need to call 'this.render();' here if already rendered
    }
}
like image 185
martin308 Avatar answered Feb 05 '26 07:02

martin308