0

这是我的代码:-

<!doctype html>
<html>
<head>
  <title></title>
</head>
<body>
    Everything related to routers....
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">    </script>
    <script src="../underscore-min.js"></script>
    <script src="../backbone-min.js"></script>
    <script type="text/javascript">

      var Router = Backbone.Router.extend({
        routes: {
            "foo/:bar" : "paramtest",
            "*action" : "func"
        },
        func: function (action) {
            console.log(action);
        },
        paramtest:function (p) {
            console.log(p);
        }
      });

      new Router();
      Backbone.history.start();
   </script>
</body>
</html>

Uncaught TypeError: Object # has no method 'on'bone-min.js:1 h.extend.start bone-min.js:1(匿名函数)

4

1 回答 1

3

当前的 Backbone 包含以下内容Backbone.history.start

if (this._hasPushState) {
  Backbone.$(window).on('popstate', this.checkUrl);
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
  Backbone.$(window).on('hashchange', this.checkUrl);
} else if (this._wantsHashChange) {
  this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
}

请注意$(window).on()调用(Backbone.$只是对通常jQuery函数的命名空间引用),这就是错误的来源。您使用的是 jQuery 1.4.4,但该on方法直到 1.7 版才添加到 jQuery。

你有两个选择:

  1. 将您的 jQuery 升级到 1.7 或更高版本,最好是最新的 jQuery。
  2. 将您的 Backbone 降级到不使用 jQuery 的on.

我不知道您会为选项 (2) 使用哪个版本,但选项 (1) 无论如何都是一个更好的主意。

于 2013-04-14T06:34:12.203 回答