如何使用Backbone Router处理此错误?

时间:2023-01-13 21:42:43

This is my application.js file

这是我的application.js文件

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

};

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

这是在调用其他packageviewer.js

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>

这是我的index.html的脚本标签,位于中

<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)')

当我运行它时,我得到TypeError:'undefined'不是一个函数(评估'parent.apply(this,arguments)')

I cannot comprehend this. Help!

我无法理解这一点。帮帮我!

1 个解决方案

#1


8  

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.

因为在这里你应该只是扩展Backbone.Router,而不是创建一个新实例。

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

当你调用`this.router = new PackageViewer()时,你在App.init中正确创建了一个新实例。

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  
});

#1


8  

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.

因为在这里你应该只是扩展Backbone.Router,而不是创建一个新实例。

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

当你调用`this.router = new PackageViewer()时,你在App.init中正确创建了一个新实例。

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  
});