var stooges = [
{name: 'moe', age: 40},
{name: 'larry', age: 50},
{name: 'curly', age: 60}
];
_.max(stooges, function(stooge){ return stooge.age; });
=> {name: 'curly', age: 60};
So curly is returned because he is the oldest, he is the max age of the above collection.
P.S (Age might not be the best example) Lets say somehow magically, larry has some kind of disease where he ages 12 years for every year and becomes 62 while curly only turns 61, how do I detect this change?
In my real app the max value in the collection will change periodically and I would like to have a function that detects a change so I can add stuff to notify the user a change in the max has been made.
I know backbone.js has changed, _previousAttributes, and other methods but they seem to only return the actual max value and not the previous max value.
Edit: I have a method in my backbone code to get get the max
getMax : function(attribute) {
var home = $('#main').attr('data-home'),
away = $('#main').attr('data-away')
return this.collection.max(function (player) {
return player.get("teamName") === home || player.get("teamName") === away? player.get(attribute) : 0;
}, this);
}
So if I want to get the maximum attribute in my collection I would call this.getMax("points"); and use that to determine who is my max point leader.
I am not sure how to compare an old value to a changed value, ex. "kevin" has 12 points while "adam" has 10, "kevin" is the current max points, so on click I add 3 points to adam he now has "13" points and is now the leader while "kevin" is the previous leader.
So I cannot figure out how to create a method or statement to detect if there is a change in max value
Edit: change handler
// 1 for one point
if(e.keyCode == 49 && !e.ctrlKey || e.keyCode == 97 && !e.ctrlKey) {
if(this.$el.hasClass('hover')) {
var addMake = parseInt(this.model.get('made_one')) + 1;
var addOnePoint = parseInt(this.model.get('points')) + 1;
this.model.save({made_one: addMake, points: addOnePoint});
feedCollection.create({
feedItem: 'made a freethrow',
playerName: this.model.get('playerName'),
teamName: this.model.get('teamName'),
facebookID: this.model.get('facebookID')
});
}
}
Okay so the actual event that changes the model is inside a separate view, it's actually a keypress event. I listen to any changes to the collection inside my statistics view which is the view that handles the max values. Inside the statistics view I render it every time there is a change, so if the values do change it automatically puts the appropriate max value, but I am unsure how to detect that change this.listenTo(this.collection, "change", this.render);.
_.maxagain in the change handler (and compare to the previous result) - you can only find larry's disease by testing them every new year. - Bergichangeevent handler (where you callthis.getMax("points"), render the template and tried to compare the previous values)? - Bergi