Last active
December 28, 2015 10:29
-
-
Save toddhgardner/7487037 to your computer and use it in GitHub Desktop.
Integrating {Track:js} TrackAll with Backbone.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OPTION 1: | |
// Automatically wrap everything | |
;(function() { | |
'use strict'; | |
if (!window.trackJs) return; | |
[ 'View' | |
, 'Model' | |
, 'Collection' | |
, 'Router' | |
].forEach(function(klass) { | |
var old = Backbone[klass]; | |
Backbone[klass] = old.extend({ | |
constructor: function() { | |
if (typeof this._trackJs === 'undefined') this._trackJs = true; | |
if (this._trackJs) trackJs.watchAll(this, 'model'); // additional parameters are excludes. Collection.model does not support watching. | |
return old.prototype.constructor.apply(this, arguments); | |
} | |
}); | |
}); | |
}).call(this); | |
// OPTION 2: | |
// Selectively wrap objects on instantiation. | |
var MyView = Backbone.View.extend({ | |
initialize: function () { | |
if (window.trackJs) { window.trackJs.watchAll(this, 'excludedFunction'); } | |
} | |
}); |
Found that comparator should also be added to the excludes list.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Thanks for this. Ported to Coffeescript here https://gist.github.com/JonMidhir/adee34b35acf87bae443