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

JAB*_*ABD 3 javascript jquery backbone.js underscore.js

这是我的application.js文件

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

};
Run Code Online (Sandbox Code Playgroud)

这是调用其他packageviewer.js

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

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

home:function(){
   $('body').append("<h1>Welcome</h1>");
 } 
});
Run Code Online (Sandbox Code Playgroud)

这是我的index.html的脚本标记 <body>

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

});
</script>
Run Code Online (Sandbox Code Playgroud)

当我跑这个我得到 TypeError: 'undefined' is not a function (evaluating 'parent.apply(this, arguments)')

我无法理解这一点.救命!

sat*_*run 8

改变这个:

window.PackageViewer = new Backbone.Router.extend({
 routes : {
   '':'home'
}
Run Code Online (Sandbox Code Playgroud)

对此:

window.PackageViewer = Backbone.Router.extend({
 routes : {
   '':'home'
}
Run Code Online (Sandbox Code Playgroud)

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

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

我使用虚拟包模型和集合测试了它并且它工作.

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

window.Packages = Backbone.Collection.extend({
    model: Package  
});
Run Code Online (Sandbox Code Playgroud)