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);
        }
    });
});
All Objects Backbone Integration
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);
        }
    });
});
All Objects Backbone Integration
["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);
        }
    });
});
All Objects Backbone Integration

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");
    }
});
Selective Backbone Integration
import { TrackJS } from "trackjs";

var MyView = Backbone.View.extend({
    initialize: function () {
        TrackJS.watchAll(this, "excludedFunction1");
    }
});
Selective Backbone Integration
var MyView = Backbone.View.extend({
    initialize: function () {
        window.trackJs && trackJs.watchAll(this, "excludedFunction1");
    }
});
Selective Backbone Integration