开发者

Cannot call 'start' of undefined when starting backbone.js history.

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.

W开发者_如何学Pythonhat could be the cause of this?

Thanks


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


I hit the same error.

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

var routerInstance = new blogRouter();


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


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


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.


A similar error message ".. has no method 'start'" pops up if you write Backbone.History.start instead of Backbone.history.start (the correct one is with lowercase h in history).


(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);


Also using old versions of jquery could cause this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜