Integrating with Backbone
Backbone defines base types that you inherit from. We can use the watchAll API to automatically wrap up your Backbone functions.
["View", "Model", "Collection", "Router"].forEach(function(type) { var BackboneType = Backbone[type]; Backbone[type] = BackboneType.extend({ constructor: function() { // Additional parameters are excluded from watching. Constructors and Comparators // have a lot of edge-cases that are difficult to wrap so we'll ignore them. window.TrackJS && TrackJS.watchAll(this, "model", "constructor", "comparator"); return BackboneType.prototype.constructor.apply(this, arguments); } }); });
import { TrackJS } from "trackjs"; ["View", "Model", "Collection", "Router"].forEach(function(type) { var BackboneType = Backbone[type]; Backbone[type] = BackboneType.extend({ constructor: function() { // Additional parameters are excluded from watching. Constructors and Comparators // have a lot of edge-cases that are difficult to wrap so we'll ignore them. TrackJS.watchAll(this, "model", "constructor", "comparator"); return BackboneType.prototype.constructor.apply(this, arguments); } }); });
["View", "Model", "Collection", "Router"].forEach(function(type) { var BackboneType = Backbone[type]; Backbone[type] = BackboneType.extend({ constructor: function() { // Additional parameters are excluded from watching. Constructors and Comparators // have a lot of edge-cases that are difficult to wrap so we'll ignore them. window.trackJs && trackJs.watchAll(this, "model", "constructor", "comparator"); return BackboneType.prototype.constructor.apply(this, arguments); } }); });
TIP If you are using Backbone Marionette, you should also exclude "childView"
and "emptyView"
from wrapping.
Alternatively, you can selectively wrap objects during initialization.
var MyView = Backbone.View.extend({ initialize: function () { window.TrackJS && TrackJS.watchAll(this, "excludedFunction1"); } });
import { TrackJS } from "trackjs"; var MyView = Backbone.View.extend({ initialize: function () { TrackJS.watchAll(this, "excludedFunction1"); } });
var MyView = Backbone.View.extend({ initialize: function () { window.trackJs && trackJs.watchAll(this, "excludedFunction1"); } });