Skip to content

Instantly share code, notes, and snippets.

@toddhgardner
Last active December 28, 2015 10:29
Show Gist options
  • Save toddhgardner/7487037 to your computer and use it in GitHub Desktop.
Save toddhgardner/7487037 to your computer and use it in GitHub Desktop.
Integrating {Track:js} TrackAll with Backbone.
// 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'); }
}
});
@marshals
Copy link

marshals commented Aug 7, 2014

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