I mostly use Vue.js due to it’s very intuitive templating system and excellent reactivity. But I still very much miss one specific feature of Backbone.js: collections.
In particular I miss the abilty to make scopes in a collection that return an instance of that collection – essentially making it function like an ActiveRecord::Collection.
Here’s an example:
Which allows me to chain them then:
And by memoizing these functions it becomes even more efficient:
That way repeatedly calling by_status(status) doesn’t trigger filtering repeatedly.
And the nuclear version that allows any filter and also caches it:
It accepts name of a function defined on the collection and then caches the results.
It also accepts name of a function defined on the model that returns boolean and also caches the results.
And finally it also accepts a function that’d be executed on the model, for example:
This brings it very close to ActiveRecord::Collection and makes things much easier (and faster). It’s particular useful when calculating statistics and collections are repeatedly filtered.