Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 33 lines (25 sloc) 1.267 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
do (Backbone) ->

  # Reference the old Collection.prototype
  proto = Backbone.Collection::

  # Methods that should trigger re-evaluation of virtuals
  writes = 'add remove reset change create sort'

  # Methods that indicate this collection is a virtual's dependency
  backboneReads = ['get', 'getByCid', 'where', 'pluck', 'clone', 'at', 'toJSON']
  underscoreReads = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find',
    'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any',
    'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex',
    'toArray', 'size', 'first', 'initial', 'rest', 'last', 'without', 'indexOf',
    'shuffle', 'lastIndexOf', 'isEmpty', 'groupBy']

  class Backbone.ViewCollection extends Backbone.Collection
    list: ->
      Backbone.Virtual.track(this, writes)
      return @models

  Backbone.Collection::model = Backbone.Model

  extendCollection = (name) ->
    Backbone.ViewCollection::[name] = ->
      Backbone.Virtual.track(this, writes)
      return proto[name].apply(this, arguments)

  # Intercept and track all read operations
  extendCollection(fn) for fn in backboneReads.concat(underscoreReads)

  # Alias ViewCollection to Collection
  Backbone.Collection = Backbone.ViewCollection
Something went wrong with that request. Please try again.