Search code examples
backbone.js

Uncaught TypeError: _.create is not a function at Function.extend backbonejs


var node = Backbone.Model.extend({
    defaults: function () {
        return {
            tag: null,
            value: null
        };
    }
});

var elements = Backbone.Collection.extend({
    model: node,
    url: "/api/xml/get",

    parse: function (data) {
        var $xml = $(data);

        return $xml.map(function () {
            var tag = $(this).each(function () {
                $(this).tagName;
            });
            return { tag: tag };
        }).get();
    },
    fetch: function (options) {
        options = options || {};
        options.dataType = "xml";
        return Backbone.Collection.prototype.fetch.call(this, options);
    }
});

var elementsView = Backbone.View.extend({
    initialize: function () {
        this.listenTo(this.collection, "sync", this.render);
    },

    render: function () {
        console.log(this.collection.toJSON());
    }
});

var eles = new elements();
new elementsView({ collection: eles });
eles.fetch();

First line errors out with the subject line using Backbone.js latest. Trying to get a simple demo working with it unable to resolve this myself. underscore.js is referenced.


Solution

  • This method was added in underscore version 1.8.3. You probably have an old version.