Axesh Ajmera
Axesh Ajmera

Reputation: 383

Error when using backbone.js router

Here is my code:-

<!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' backbone-min.js:1 h.extend.start backbone-min.js:1 (anonymous function)

Upvotes: 0

Views: 295

Answers (1)

mu is too short
mu is too short

Reputation: 434935

The current Backbone contains this inside 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);
}

Note the $(window).on() calls (Backbone.$ is just a namespaced reference to the usual jQuery function), that's where the error is coming from. You're using jQuery 1.4.4 but the on method wasn't added to jQuery until version 1.7.

You have two options:

  1. Upgrade your jQuery to 1.7 or higher, preferably the latest jQuery.
  2. Downgrade your Backbone to a version that doesn't use jQuery's on.

I don't know which version you'd use for option (2) but option (1) is a better idea anyway.

Upvotes: 3

Related Questions