JABD
JABD

Reputation: 640

How do I deal with this error with Backbone Router?

This is my application.js file

window.App = {
init: function() {
     this.router = new PackageViewer();
       Backbone.history.start();
   }

};

This is calling the other packageviewer.js that's here

window.PackageViewer = new Backbone.Router.extend({
 routes : {
   '':'home'
},

initialize:function(){
   this.collection = new Package();
},

home:function(){
   $('body').append("<h1>Welcome</h1>");
 } 
});

And here is my index.html's script tag that is inside <body>

<script type="text/javascript"> 
$(document).ready(function (){            
    App.init();

});
</script>

When I run this I get TypeError: 'undefined' is not a function (evaluating 'parent.apply(this, arguments)')

I cannot comprehend this. Help!

Upvotes: 3

Views: 4281

Answers (1)

satchmorun
satchmorun

Reputation: 12477

Change this:

window.PackageViewer = new Backbone.Router.extend({
 routes : {
   '':'home'
}

to this:

window.PackageViewer = Backbone.Router.extend({
 routes : {
   '':'home'
}

Because here you're supposed to be just extending Backbone.Router, not creating a new instance.

You correctly create a new instance in your App.init when you call `this.router = new PackageViewer();

I tested this with a dummy Package Model and Collection and it worked.

window.Package = Backbone.Model.extend({
});

window.Packages = Backbone.Collection.extend({
    model: Package  
});

Upvotes: 8

Related Questions