Permalink
Browse files

translated backbone.virtual

  • Loading branch information...
1 parent 8164e3c commit 673a33082e0b140786310b97fd7f369e9f900f9e @hunterloftis committed May 18, 2012
Showing with 190 additions and 191 deletions.
  1. +73 −0 backbone.virtual.coffee
  2. +0 −100 backbone.virtual.js
  3. +117 −91 lib/backbone.virtual.js
View
73 backbone.virtual.coffee
@@ -0,0 +1,73 @@
+do (Backbone) ->
+
+ class Backbone.Virtual
+
+ @_computations = []
+ @_dependencies = undefined
+
+ @startTracking: ->
+ Backbone.Virtual._dependencies = []
+ Backbone.Virtual._computations.push(Backbone.Virtual._dependencies)
+
+ @stopTracking: ->
+ computations = Backbone.Virtual._computations
+ dependencies = computations.pop()
+ if computations.length
+ Backbone.Virtual._dependencies = computations[computations.length - 1]
+ else
+ Backbone.Virtual._dependencies = undefined
+ return dependencies
+
+ @track: (model, event) ->
+ Backbone.Virtual._dependencies?.push(
+ model: model
+ event: event
+ )
+
+ constructor: (options) ->
+ _.extend(this, options)
+ @dependencies = []
+ @result = undefined
+
+ runSafe: =>
+ try
+ return @get.call(@model, @attr, this)
+ catch error
+ return @fail
+
+ run: =>
+ Backbone.Virtual.startTracking()
+ if @hasOwnProperty('fail')
+ @result = @runSafe()
+ else
+ @result = @get.call(@model, @attr, this)
+ @update(Backbone.Virtual.stopTracking())
+ @trigger('change', this)
+ return @result
+
+ onChange: =>
+ @run()
+
+ update: (newDependencies) =>
+ @remove(dep) for dep in @dependencies
+ @add(dep) for dep in newDependencies
+ @dependencies = newDependencies
+
+ add: (dependency) =>
+ dependency.model.on(dependency.event, @onChange)
+
+ remove: (dependency) =>
+ dependency.model.off(dependency.event, @onChange)
+
+ get: (attr, virtual) ->
+ if typeof virtual.reference == 'function' then model = virtual.reference.call(this)
+ else model = virtual.reference
+ return model.get(attr)
+
+ set: (attr, val, options, virtual) ->
+ if typeof virtual.reference == 'function' then model = virtual.reference.call(this)
+ else model = virtual.reference
+ return model.set(attr, val)
+
+ _.extend(Backbone.Virtual::, Backbone.Events)
+
View
100 backbone.virtual.js
@@ -1,100 +0,0 @@
-(function(Backbone) {
-
- // Options = model, attr, get, set, fail
- Backbone.Virtual = function(options) {
- _.extend(this, options);
- this.dependencies = [];
- this.result = undefined;
- };
-
- Backbone.Virtual._dependencies = [];
- Backbone.Virtual._computations = [];
-
- _.extend(Backbone.Virtual.prototype, Backbone.Events, {
-
- run: function() {
- Backbone.Virtual.startTracking();
- if (this.hasOwnProperty('fail')) {
- try {
- this.result = this.get.call(this.model, this.attr, this);
- }
- catch (e) {
- this.result = this.fail;
- }
- }
- else {
- this.result = this.get.call(this.model, this.attr, this);
- }
- this.update(Backbone.Virtual.stopTracking());
- this.trigger('change', this);
- return this.result;
- },
-
- onChange: function() {
- this.run();
- },
-
- update: function(newDependencies) {
- _.each(this.dependencies, this.remove, this);
- _.each(newDependencies, this.add, this);
- this.dependencies = newDependencies;
- },
-
- remove: function(dependency) {
- dependency.model.off(dependency.event, this.onChange, this);
- },
-
- add: function(dependency) {
- dependency.model.on(dependency.event, this.onChange, this);
- },
-
- // Default virtual .get()
- // TODO: remove the typeof checks in the getters/setters
- get: function(attr, virtual) {
- var model = (typeof virtual.reference === 'function') ?
- virtual.reference.call(this) : virtual.reference;
- return model.get(attr);
- },
-
- // Default virtual .set()
- // TODO: remove the typeof checks in the getters/setters
- set: function(attr, val, options, virtual) {
- var model = (typeof virtual.reference === 'function') ?
- virtual.reference.call(this) : virtual.reference;
- return model.set(attr, val);
- }
-
- });
-
- _.extend(Backbone.Virtual, {
- _computations: [],
- _dependencies: undefined,
-
- startTracking: function() {
- // Create a new array for tracking dependencies of this compute function
- Backbone.Virtual._dependencies = [];
- // Push the new tracking array onto the stack of computations
- Backbone.Virtual._computations.push(Backbone.Virtual._dependencies);
- },
-
- stopTracking: function() {
- // Pop the tracking array off of the stack
- var dependencies = Backbone.Virtual._computations.pop();
- // Point the tracking array to the next item on the stack
- Backbone.Virtual._dependencies = Backbone.Virtual._computations.length ?
- Backbone.Virtual._computations[Backbone.Virtual._computations.length - 1] :
- undefined;
- return dependencies;
- },
-
- track: function(model, event) {
- if (Backbone.Virtual._dependencies) {
- Backbone.Virtual._dependencies.push({
- model: model,
- event: event
- });
- }
- }
- });
-
-})(Backbone);
View
208 lib/backbone.virtual.js
@@ -1,100 +1,126 @@
-(function(Backbone) {
+// Generated by CoffeeScript 1.3.3
+(function() {
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+
+ (function(Backbone) {
+ Backbone.Virtual = (function() {
+
+ Virtual._computations = [];
+
+ Virtual._dependencies = void 0;
+
+ Virtual.startTracking = function() {
+ Backbone.Virtual._dependencies = [];
+ return Backbone.Virtual._computations.push(Backbone.Virtual._dependencies);
+ };
+
+ Virtual.stopTracking = function() {
+ var computations, dependencies;
+ computations = Backbone.Virtual._computations;
+ dependencies = computations.pop();
+ if (computations.length) {
+ Backbone.Virtual._dependencies = computations[computations.length - 1];
+ } else {
+ Backbone.Virtual._dependencies = void 0;
+ }
+ return dependencies;
+ };
+
+ Virtual.track = function(model, event) {
+ var _ref;
+ return (_ref = Backbone.Virtual._dependencies) != null ? _ref.push({
+ model: model,
+ event: event
+ }) : void 0;
+ };
+
+ function Virtual(options) {
+ this.remove = __bind(this.remove, this);
- // Options = model, attr, get, set, fail
- Backbone.Virtual = function(options) {
- _.extend(this, options);
- this.dependencies = [];
- this.result = undefined;
- };
+ this.add = __bind(this.add, this);
- Backbone.Virtual._dependencies = [];
- Backbone.Virtual._computations = [];
+ this.update = __bind(this.update, this);
- _.extend(Backbone.Virtual.prototype, Backbone.Events, {
+ this.onChange = __bind(this.onChange, this);
- run: function() {
- Backbone.Virtual.startTracking();
- if (this.hasOwnProperty('fail')) {
+ this.run = __bind(this.run, this);
+
+ this.runSafe = __bind(this.runSafe, this);
+ _.extend(this, options);
+ this.dependencies = [];
+ this.result = void 0;
+ }
+
+ Virtual.prototype.runSafe = function() {
try {
+ return this.get.call(this.model, this.attr, this);
+ } catch (error) {
+ return this.fail;
+ }
+ };
+
+ Virtual.prototype.run = function() {
+ Backbone.Virtual.startTracking();
+ if (this.hasOwnProperty('fail')) {
+ this.result = this.runSafe();
+ } else {
this.result = this.get.call(this.model, this.attr, this);
}
- catch (e) {
- this.result = this.fail;
+ this.update(Backbone.Virtual.stopTracking());
+ this.trigger('change', this);
+ return this.result;
+ };
+
+ Virtual.prototype.onChange = function() {
+ return this.run();
+ };
+
+ Virtual.prototype.update = function(newDependencies) {
+ var dep, _i, _j, _len, _len1, _ref;
+ _ref = this.dependencies;
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ dep = _ref[_i];
+ this.remove(dep);
}
- }
- else {
- this.result = this.get.call(this.model, this.attr, this);
- }
- this.update(Backbone.Virtual.stopTracking());
- this.trigger('change', this);
- return this.result;
- },
-
- onChange: function() {
- this.run();
- },
-
- update: function(newDependencies) {
- _.each(this.dependencies, this.remove, this);
- _.each(newDependencies, this.add, this);
- this.dependencies = newDependencies;
- },
-
- remove: function(dependency) {
- dependency.model.off(dependency.event, this.onChange, this);
- },
-
- add: function(dependency) {
- dependency.model.on(dependency.event, this.onChange, this);
- },
-
- // Default virtual .get()
- // TODO: remove the typeof checks in the getters/setters
- get: function(attr, virtual) {
- var model = (typeof virtual.reference === 'function') ?
- virtual.reference.call(this) : virtual.reference;
- return model.get(attr);
- },
-
- // Default virtual .set()
- // TODO: remove the typeof checks in the getters/setters
- set: function(attr, val, options, virtual) {
- var model = (typeof virtual.reference === 'function') ?
- virtual.reference.call(this) : virtual.reference;
- return model.set(attr, val);
- }
-
- });
-
- _.extend(Backbone.Virtual, {
- _computations: [],
- _dependencies: undefined,
-
- startTracking: function() {
- // Create a new array for tracking dependencies of this compute function
- Backbone.Virtual._dependencies = [];
- // Push the new tracking array onto the stack of computations
- Backbone.Virtual._computations.push(Backbone.Virtual._dependencies);
- },
-
- stopTracking: function() {
- // Pop the tracking array off of the stack
- var dependencies = Backbone.Virtual._computations.pop();
- // Point the tracking array to the next item on the stack
- Backbone.Virtual._dependencies = Backbone.Virtual._computations.length ?
- Backbone.Virtual._computations[Backbone.Virtual._computations.length - 1] :
- undefined;
- return dependencies;
- },
-
- track: function(model, event) {
- if (Backbone.Virtual._dependencies) {
- Backbone.Virtual._dependencies.push({
- model: model,
- event: event
- });
- }
- }
- });
+ for (_j = 0, _len1 = newDependencies.length; _j < _len1; _j++) {
+ dep = newDependencies[_j];
+ this.add(dep);
+ }
+ return this.dependencies = newDependencies;
+ };
+
+ Virtual.prototype.add = function(dependency) {
+ return dependency.model.on(dependency.event, this.onChange);
+ };
+
+ Virtual.prototype.remove = function(dependency) {
+ return dependency.model.off(dependency.event, this.onChange);
+ };
+
+ Virtual.prototype.get = function(attr, virtual) {
+ var model;
+ if (typeof virtual.reference === 'function') {
+ model = virtual.reference.call(this);
+ } else {
+ model = virtual.reference;
+ }
+ return model.get(attr);
+ };
+
+ Virtual.prototype.set = function(attr, val, options, virtual) {
+ var model;
+ if (typeof virtual.reference === 'function') {
+ model = virtual.reference.call(this);
+ } else {
+ model = virtual.reference;
+ }
+ return model.set(attr, val);
+ };
+
+ return Virtual;
+
+ })();
+ return _.extend(Backbone.Virtual.prototype, Backbone.Events);
+ })(Backbone);
-})(Backbone);
+}).call(this);

0 comments on commit 673a330

Please sign in to comment.