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

发布于 2024-12-03 15:12:20 字数 206 浏览 4 评论 0原文

我在调用时得到 Cannot call 'start' of undefined...

    Backbone.history.start()

运行某些检查时,Backbone 返回对象,但 Backbone.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.

What could be the cause of this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

轻许诺言 2024-12-10 15:12:20

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

更多的:

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

嗯,由于某种原因 Backbone.history 未定义,所以没有
start方法就可以了。原来Backbone.js创建了一个实例
Backbone.History(大写“H”)的称为 Backbone.history(小写
case 'h') 一旦创建了一个至少有一个控制器
上面指定的路线。这是有道理的,因为历史管理只是
如果有要响应的路由,则为必需。

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

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

画尸师 2024-12-10 15:12:20

我遇到了同样的错误。

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

var routerInstance = new blogRouter();

I hit the same error.

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

var routerInstance = new blogRouter();
时光磨忆 2024-12-10 15:12:20

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

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

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
就此别过 2024-12-10 15:12:20

确保在 HTML 头中包含 jQuery.js before underscore.jsbackbone.js

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

疯到世界奔溃 2024-12-10 15:12:20

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

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.

懒的傷心 2024-12-10 15:12:20

如果您编写 Backbone.History.start 而不是 Backbone.history.start(正确的是在 history 中使用小写的 h)。

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).

如果没结果 2024-12-10 15:12:20
(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);
(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);
魔法少女 2024-12-10 15:12:20

使用旧版本的 jquery 也可能会导致此问题

Also using old versions of jquery could cause this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文