Search code examples
javascriptbackbone.js

BackboneJS: getting uncaught type error in backbone.js


I am new to backbonejs. I am facing the following error

Uncaught TypeError: undefined is not a function                    backbone.js:26

Here is my code

index.php inside script tag

<script>
    new App.Router;
    Backbone.history.start();

    App.contacts = new App.Collections.Contacts;
    App.contacts.fetch().then(function () {
        new App.Views.App({ collection: App.contacts });
    });
</script>

main.js

(function ($) {
window.App = {
    Models: {},
    Collections: {},
    Views: {},
    Router: {}
};

window.vent = _.extend({}, Backbone.Events);
})(jQuery);

router.js

App.Router = Backbone.Router.extend({
routes: {
    '': 'index'
},

index: function () {
    console.log('index page');
}
});

collections.js

App.Collections.Contacts = Backbone.Collection.extend({
model: App.Models.Contact,
url : '/contacts'
});

models.js

App.Models.Appoinment = Backbone.Model.extend({
//validate
});

views.js

//Global App view
App.Views.App = Backbone.View.extend({
initialize: function () {
    console.log(this.collection.toJSON());
}
});

What am I doing wrong here?


Solution

  • I found the solution. Here what I did Inside collections.js I was pointing to App.Models.Contact whereas I did not have this model. I created it and now it works.