gmac / backbone.epoxy

Declarative data binding and computed models for Backbone
http://epoxyjs.org
MIT License
615 stars 89 forks source link

Model/collection type checks should be configurable #116

Open markerikson opened 9 years ago

markerikson commented 9 years ago

I previously reported issue #43, where several places in the codebase had hardcoded references to Backbone.Model. That issue was fixed (thanks), but I've just run into a similar issue.

Epoxy currently uses two internal functions, isModel() and isCollection(), to determine how to handle binding source setup (per https://github.com/gmac/backbone.epoxy/blob/7aeb9f70a14b5b049dbb204c8b4a8578e0e386d6/backbone.epoxy.js#L636 ). However, both those functions still have hardcoded references to Backbone.Model and Backbone.Collection.

I've started to use parts of the Ampersand library in my current codebase, including Ampersand-State (a forked and upgraded version of Backbone.Model). Amp.State still supports the same get()/set()/toJSON() API as Backbone.Model, so the core Epoxy binding logic should still work correctly. However, as currently written, the isModel() and isCollection() functions both fail when attempting to add an Amp.State instance to the binding context, as it is not a Backbone.Model.

I was able to get Epoxy and Ampersand-State to cooperate by hacking up my local copy of Epoxy to look like this:

    var isModel = function(obj) {
        var isBackboneModel = (obj instanceof Backbone.Model);
        var isAmpersandModel = (_.result(obj, "getType") == "model");
        return  isBackboneModel || isAmpersandModel;
    };

I really don't like using locally-modified copies of dependencies, though, so it would be nice if there was a way to override the built-in logic.

A comment on the previous issue links to a fork commit that exposes several inheritance-related pieces on the public Epoxy object. That commit appears to predate the official commit for #43, but the approach looks reasonable.

So, would it be possible to expose isModel() and isCollection() publicly so that they could be overwritten?