I have implemented the following functionality:
define([
'jquery',
'underscore',
'backbone',
'marionette',
'app',
'views/y/x/FlightScheduleLayoutView',
'collections/y/x/ScheduleCollection'
], function($, _, Backbone, Marionette, App, FlightScheduleLayout, ScheduleCollection) {
var FlightScheduleListView = Backbone.Marionette.CollectionView.extend({
collection: ScheduleCollection,
itemView: FlightScheduleLayout,
doSort: function(sortCode, sortDirection) {
this.collection.setSortField(sortCode, sortDirection);
this.collection.sort();
this.collection.trigger('reset');
},
onAfterItemAdded: function(itemView) {
if ($(itemView.el).hasClass("flight")) {
var flight = $(itemView.el);
var flightDetailsBtn = flight.find(".flight_details");
var flightDetails = flight.find(".option_details");
var count = parseInt(this.children.length);
var flightDetailsId = "flight-details-" + count;
flightDetailsBtn.attr("data-target", "#" + flightDetailsId);
flightDetails.attr("id", flightDetailsId);
flight.find("[data-toggle=tooltip]").tooltip();
}
}
});
return FlightScheduleListView;
});
Now there is a functionality somewhere in js that does the following:
this.flightScheduleListView = new FlightScheduleListView({ collection: schedules });
Add later in code this happens:
this.flightScheduleListView.collection.add(this.flightSchedules.models.slice(this.numberOfFlightSchedulesVisibleCurrently, this.numberOfFlightSchedulesVisibleCurrently + this.flightSchedulesPerPage));
Using OnAfterItemAdded
works perfectly. But this is time consuming as it iterates over every view. Is there any render function that gets called when we add something to the collection of the view? I can't use OnRender
because it's only called on View instantiation and not when adding to collection.