Search code examples
javascriptjquerybackbone.js

Backbone Undefined is not a function


I have a really simple backbone app, that returns:

undefined is not a function 

in the console. Everything seems to be defined appropriately. Any idea what the issue could be?

js:

$(function () {

    var myModel = Backbone.Model.extend({});

    var myCollection = Backbone.Collection.extend({
        doStuff: function () {
            alert("doing Stuff");
        },
        doOtherStuff: function () {
            alert("doing other stuff");
        }
    });

    var myCollectionView = Backbone.View.extend({
        template: "<button id='trigger'>Trigger</button>",
        initialize: function () {
            this.listenTo(this.collection, 'sync', this.collection.doStuff);
            this.listenTo(this.collection, 'sync', this.collection.doOtherStuff);
            this.render();
        },
        events: {
            "click #trigger": "trigger"
        },
        render: function () {
            $("#app").html(this.template);
        },
        trigger: function () {
            alert("triggering");
            $(this).trigger("sync");
        }

    });

    var mymodel = new myModel({});

    var mycollection = new myCollection({
        model: mymodel
    });

    var mycollectionview = new myCollectionView({
            collection: mycollection
    });
});

Reproduced in a fiddle: http://jsfiddle.net/5C238/1/


Solution

  • You're calling in the wrong order

    <script src="//jashkenas.github.io/underscore/underscore-min.js"></script>
    <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="//jashkenas.github.io/backbone/backbone-min.js"></script>
    

    http://jsfiddle.net/LG38U/