Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last item view from CompositeView in Marionette

Is it possible to get the last ItemView from a Backbone CompositeView? I've found a lot of documentation for getting the last model in a Collection, but not that last View in a Collection of Views.

The reason I would like to do this is so I can render the last row in a table slightly differently.

The following is the code I'm using right now, it works fine, but it would be less "hacky" if I could get the correct ItemView from the CompositeView that created and rendered it. It uses jQuery to search the entire part of the DOM contained by the CompositeView for the last element, then manipulates that element.

B.ListControl.View = Backbone.Marionette.CompositeView.extend({
    itemView: ...,
    itemViewContainer: ...,
    template: ...,
    // ON RENDER
    onRender: function(){
        // Add button to the last element in the list
        this.$el.find('div:last').text('Custome stuff goes here');
    }
});

Thanks!

like image 605
Chris Dutrow Avatar asked Sep 01 '13 05:09

Chris Dutrow


1 Answers

When your collection is fetched you can get last item in this way:

this.children.findByIndex(this.children.length - 1);

Babysitter plugin provides a lot of useful methods for you:

findByModel, findByCollection, findByCustom, findByIndex, findByCid

like image 93
Vitalii Petrychuk Avatar answered Nov 15 '22 10:11

Vitalii Petrychuk