Search code examples
javascriptbackbone.jsunderscore.js

backbone _.chain(...) is not a function


I am trying to use _chain, but I got:

_.chain(...).getUniq is not a function My code:

var Offer = Document.extend({
    default: {
        name: null,
    },

    set: function (attributes, options) {
        Backbone.Model.prototype.set.apply(this, arguments);
    }
});

 var Offers = Backbone.Collection.extend({
    getUniq: function () {
        return _.uniq(this.pluck("name'));
    },
    model: Offer
});

var offers = new Offers;
offers.add(offer1);
_.chain(offers).getUniq()

Solution

  • That's because getUniq is not an underscore function.

    The _.chain() function returns an underscoreJS object:

    _.chain = function(obj) {
        var instance = _(obj);
        instance._chain = true;
        return instance;
      };