Permalink
Browse files

translated backbone.viewcollection to CS

  • Loading branch information...
1 parent e731c97 commit 8164e3c10642150e20444232d60f223b896a828d @hunterloftis committed May 18, 2012
Showing with 74 additions and 82 deletions.
  1. +33 −0 backbone.viewcollection.coffee
  2. +0 −42 backbone.viewcollection.js
  3. +41 −40 lib/backbone.viewcollection.js
@@ -0,0 +1,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
View
@@ -1,42 +0,0 @@
-(function(Backbone) {
-
- // Store reference to old methods
- var proto = Backbone.Collection.prototype;
-
- // Writes: methods that should trigger re-evaluation of dependent values
- var writes = 'add remove reset change create sort';
-
- // Reads: methods that indicate this collection is a dependency of another value
- var backboneReads = ['get', 'getByCid', 'where', 'pluck', 'clone', 'at', 'toJSON'];
- var 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'];
-
- Backbone.ViewCollection = Backbone.Collection.extend({
- // TODO: ViewCollection methods
- list: function() {
- Backbone.Virtual.track(this, writes);
- return this.models;
- }
- });
-
- // Use the external, monkeypatched version of Backbone.Model instead of the internal closed-over Model
- Backbone.Collection.prototype.model = Backbone.Model;
-
- // Add tracking to Backbone.Collection
- function extendCollection(name) {
- Backbone.ViewCollection.prototype[name] = function() {
- // Trigger an update on any write operation...
- Backbone.Virtual.track(this, writes);
- return proto[name].apply(this, arguments);
- };
- }
- // ...if a Virtual function performs a Collection read operation:
- _.each(backboneReads.concat(underscoreReads), extendCollection);
-
- // Alias ViewCollection to Collection
- Backbone.Collection = Backbone.ViewCollection;
-
-})(Backbone);

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 8164e3c

Please sign in to comment.