简体   繁体   English

如何使用主干路由器处理此错误?

[英]How do I deal with this error with Backbone Router?

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>这是我在<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' is not a function (evaluating 'parent.apply(this, arguments)')

I cannot comprehend this.我无法理解这一点。 Help!帮助!

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.我用一个虚拟 Package Model 和 Collection 对此进行了测试,它工作正常。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM