javascript 启动backbone.js 历史时无法调用未定义的“开始”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7343357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:47:37  来源:igfitidea点击:

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

javascriptbackbone.jscoffeescript

提问by fancy

I get Cannot call 'start' of undefinedwhen calling...

Cannot call 'start' of undefined打电话的时候...

    Backbone.history.start()

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

运行一些检查时,Backbone 返回对象,但 Backbone.history 返回未定义。

What could be the cause of this?

这可能是什么原因?

Thanks

谢谢

回答by wesbos

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

控制器上有路由吗?Backbone 仅在至少指定了一条路由后才创建历史记录。

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.

类型错误:无法调用未定义的方法“开始”**

嗯,由于某种原因 Backbone.history 是未定义的,所以它没有 start 方法。事实证明,一旦创建了至少指定了一个路由的控制器,Backbone.js 就会创建一个名为 Backbone.history(小写 'h')的 Backbone.History(大写 'H')实例。这是有道理的,因为只有在有路由响应时才需要历史管理。

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

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

回答by chrisftw

I hit the same error.

我遇到了同样的错误。

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

当您不创建路由器实例时会发生这种情况:

var routerInstance = new blogRouter();

回答by mgadda

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.

如果您使用的是 CoffeeScript 并且您的路由器是使用 CoffeeScript 类关键字和构造函数方法定义的,请仔细检查您是否在构造函数的定义中调用了 super。这解决了 Backbone.history 为空的问题。

class AppRouter extends Backbone.Router
  constructor: (options) ->
    super(options)
    # other initialization code

回答by Rok Krulec

Make sure you include jQuery.jsbeforeunderscore.jsand backbone.jsin HTML head.

确保在 HTML 头部jQuery.js之前underscore.jsbackbone.js中包含。

回答by Chandu

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.

由于不同的原因,我遇到了完全相同的问题,花了几个小时后我意识到我的路由器“路由”中的一个方法:function() 导致了这个问题,在我评论这个不必要的额外方法之后,history.start() 按预期工作.

回答by Musikar

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

回答by jbasko

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

如果你写一个类似的错误消息“..有没有方法‘开始’”弹出Backbone.History.start,而不是Backbone.history.start(正确的是小写hhistory)。

回答by Houman

Also using old versions of jquery could cause this

也使用旧版本的 jquery 可能会导致这个