So below is just a rough idea of what I am doing, the collection is nested and I need to call getMax()
when this.collection.get('players')
changes
module.exports = LeadersCollectionView = Marionette.CompositeView.extend({
className: 'live-stats',
template: require('../../../templates/leaders/live-stats.hbs'),
initialize: function() {
this.listenTo(this.collection, 'change', this.render);
//this.listenTo(this.collection.get('players'), 'change', this.render);
},
getMax : function(attribute) {
console.log('getMax called')
var home = this.collection.get('5368dcc1227a937829b2cb4a').players.models;
var away = this.collection.get('5368dcd9227a937829b2cb4c').players.models;
leaders = new PlayersCollection(home.concat(away))
return leaders.max(function(leader) {
return leader.get(attribute);
});
},
templateHelpers:function(){
return {
maxPoints: this.getMax('points').get('player_name')
}
},
itemView: leadersView,
appendHtml: function(collectionView, itemView){
collectionView.$('.live-data .created-teams').append(itemView.el);
}
});
I was using backbone-relational, and then decided to drop it, now I think I may need it again, but hopefully there is an easier way.
So while there may be a better way to write the getMax()
function (obviously static teams passed in as of now) I can't really use it if I cant update the getMax()
when the points are changing within the players models.
So my question can I listen to changes on a nested model without backbone-relational and how would I do that?