【问题标题】:Backbone.history.start() giving out errorBackbone.history.start() 给出错误
【发布时间】:2015-04-10 13:03:26
【问题描述】:

得到一个错误:

Uncaught TypeError: undefined is not a function

匿名函数错误指向

Backbone.history.start()

下面是我的 index.html 和 main.js。

我有一种感觉,jquery、下划线和主干文件可能无法正确加载,因此发生了此错误 骨干中的初学者。非常感谢任何帮助

使用的版本:

underscore - 1.8.3
backbone - 1.1.2

index.html

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="/stylesheets/style.css">
        <script src="javascripts/jquery.min.js"></script>
        <script src="javascripts/json2.js"></script>
        <script src="javascripts/underscore-min.js"></script>
        <script src="javascripts/backbone-min.js"></script>
        <script src="javascripts/main.js"></script>
    </head>
</html>

ma​​in.js

    $(document).ready(function(){

    var Theater = {
        Models: {},
        Collections: {},
        Views: {},
        Templates:{},
        Routers:{}
    }

    Theater.Models.Movie = Backbone.Model.extend({});


    Theater.Collections.Movies = Backbone.Collection.extend({
        model: Theater.Models.Movie,
        url: "/json",
        initialize: function(){
            console.log("Movies initialize")
        }
    });


    Theater.Routers = Backbone.Router.extend({
        initialize:function(){  console.log("defaultRoute");},
        routes: {
            "": "defaultRoute" 
        },
        defaultRoute: function () {
            console.log("defaultRoute");
        }
    });

    console.log("gonna call approuter");
    var appRouter = new Theater.Routers();
    Backbone.history.start();

   });

【问题讨论】:

  • 您应该在控制台中单击它所指的行。它可能会提供有关确切行的更多信息(如果适用)
  • @Seth:是的,我做到了,这就是我理解 Backbone.history.start() 有一些问题的方式:)
  • 所以它所指的行是Backbone.history.start()?您应该尝试在其上放置一个断点并单步执行所有函数调用以查看它在哪里中断。

标签: backbone.js backbone-routing


【解决方案1】:

我猜你可能使用的是相当旧的 jQuery 版本,可能是 1.6?

1.6 不包含on 监听事件的方法,所以我猜当你调用Backbone.history.start() 时,Backbone 在尝试做的时候会抛出一个错误:

Backbone.$(window).on('hashchange', this.checkUrl);

升级您的 jQuery 版本。如果需要支持 IE 6/7/8 升级到 jQuery 1.x,如果不需要升级到 2.x。

【讨论】:

    猜你喜欢
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2012-09-25
    • 2013-03-21
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多