Cannot call 'start' of undefined when star

2019-01-23 11:09发布

I get Cannot call 'start' of undefined when calling...

    Backbone.history.start()

When running some checks Backbone returns the object but Backbone.history returns undefined.

What could be the cause of this?

Thanks

8条回答
家丑人穷心不美
2楼-- · 2019-01-23 11:25

I faced exact same issue for different reason, after spending couple of hours i realized that a method in my router "route" : function() is causing the issue, after i comment this unnecessary extra method, history.start() worked as expected.

查看更多
混吃等死
3楼-- · 2019-01-23 11:28

Make sure you include jQuery.js before underscore.js and backbone.js in HTML head.

查看更多
神经病院院长
4楼-- · 2019-01-23 11:40
(function($) {

    var Demo = Backbone.Router.extend({
        routes:{
            '*actions':'defaultRoute'
        },
        defaultRoute:function(action){
            alert('xss');
        }
    });

    var demo = new Demo;
    Backbone.history.start();
    //My site http://bbs.w3hacker.com

})(jQuery);
查看更多
唯我独甜
5楼-- · 2019-01-23 11:43

I hit the same error.

This happens when you do not make an instance of the router:

var routerInstance = new blogRouter();
查看更多
6楼-- · 2019-01-23 11:48

If you're using CoffeeScript and your Router is defined using the CoffeeScript class keyword with a constructor method, double check that you invoke super in the definition of the constructor. This resolved the issue of Backbone.history being null for me.

class AppRouter extends Backbone.Router
  constructor: (options) ->
    super(options)
    # other initialization code
查看更多
做自己的国王
7楼-- · 2019-01-23 11:50

Do you have any routes on the controller? Backbone only creates the history once at least one route is specified.

More:

TypeError: Cannot call method 'start' of undefined**

Hmm, for some reason Backbone.history is undefined, and so there is no start method on it. It turns out that Backbone.js creates an instance of Backbone.History (upper case ‘H’) called Backbone.history (lower case ‘h’) once a controller has been created that has at least one route specified on it. This makes sense, as history management is only required if there are routes to respond to.

http://tinnedfruit.com/2011/04/26/testing-backbone-apps-with-jasmine-sinon-3.html

查看更多
登录 后发表回答