countBy not one of the Underscore methods mixed with collections , so as you saw, this will not work:
T.collection.countBy(function(model){ return model.get('text') });
And the collection is not an array, so this will not work either:
_.countBy(T.collection,function(model){ return model.get('text') });
When you do this, model will not be the model in the collection, it will be one of the property values ββof the T.collection object; for example this:
_({where: 'is', pancakes: 'house?'}).countBy(function(x) { console.log(x); return 0 });βββ
will give you is and house? in the console.
However, T.collection.models is an array, an array of models. This means that this should work:
_.countBy(T.collection.models, function(model) { return model.get('text') });
I would recommend adding this as a method to your collection so that outsiders can not get around the property of the models collection.
source share