简体   繁体   English

Backbone.history已经启动

[英]Backbone.history has already been started

I am getting an error "Backbone.history has already been started" on my Backbone.js app. 我在Backbone.js应用程序上收到错误“Backbone.history已经启动”。 Here's my code. 这是我的代码。

(function ($) {
    // model for each article
    var Article = Backbone.Model.extend({});

    // collection for articles
    var ArticleCollection = Backbone.Collection.extend({
        model: Article
    });

    // view for index page
    var MainView = Backbone.View.extend({
        el: $('#wrapper'),
        render: function (){
            var template = Handlebars.compile($("#main_hb").html());
            $(this.el).find('#main_content').html(template);
            return this;            
        }   
    });

    // view for listing blog articles
    var ArticleListView = Backbone.View.extend({
        el: $('#wrapper'),
        render: function(){
            var js = this.model.toJSON();
            var template = Handlebars.compile($("#articles_hb").html());
            $(this.el).find('#main_content').html(template({articles: js}));
            return this;    
        }
    });

    // main app
    var ArticleApp = Backbone.Router.extend({
        // setup routes
        routes: {
            ""  : "index",
            "blog"  : "blog"
        },          
        index: function(){
            var main = new MainView({});
            main.render();
            return this;
        },          
        blog: function(){
            $.ajax({
                url: 'blogs/articles', 
                dataType: 'json',
                data: {},
                success: function(data)
                {
                    var articles = new ArticleCollection(data);
                    var view = new ArticleListView({model: articles});
                    view.render();
                }       
            });
            return this;
        }

    });

    // let's start the app!
    articleApp = new ArticleApp();
    Backbone.history.start();

})(jQuery);

The app itself seems like it's working fine. 该应用程序本身似乎工作正常。 But that error in Chrome is mysterious. 但Chrome中的错误是神秘的。

12/30/2013 UPDATE: looks like this might be a common issue, i decide to bold the reason. 12/30/2013更新:看起来这可能是一个常见的问题,我决定大胆的原因。

I ran into same issue and solved it. 我遇到了同样的问题并解决了它。 The reason is I 原因是我

imported the js file twice

So I may suggest you check the page which contains this script see if it was introduced twice. 因此,我建议您检查包含此脚本的页面,看看它是否已经两次引入。

Yujings said it right! Yujings说对了! It imported the js twice. 它导入了两次js。 updated My "fix" (is definitely more of a workaround) was to place this in the router. 更新我的“修复”(肯定更像是一种解决方法)是将其置于路由器中。

Backbone.history.stop();
Backbone.history.start();

Probably not the most ideal, but it got the job done in a naive fashion. 可能不是最理想的,但它以天真的方式完成了工作。

I had same problem because of Turbolinks in my Rails 4 app. 由于我的Rails 4应用程序中的Turbolinks,我遇到了同样的问题。 This workaround helped me. 这种解决方法帮助了我。 Maybe it will help you too: 也许它也会对你有所帮助:

initialize: ->
  new MyApp.Routers.Posts
  Backbone.history.start() unless Backbone.History.started
  $(document).on "page:change", ->
    Backbone.history.stop()
    Backbone.history.start()

Found this answer here: https://github.com/codebrew/backbone-rails/issues/134 在这里找到这个答案: https//github.com/codebrew/backbone-rails/issues/134

Your code seems to be ok. 你的代码似乎还可以。 Are you sure you load it exactly once? 你确定你加载了一次吗? If you put console.log() just before history.start(), is the log printed exactly once? 如果你把history.log()放在history.start()之前,那么日志是否只打印一次?

In general you get this error, when history.start() is called multiple times. 通常,当多次调用history.start()时会出现此错误。 You need to locate the second call, I'm afraid. 我担心你需要找到第二个电话。 The code you posted here is not enough to find it. 您在此处发布的代码不足以找到它。

I ran into this issue and it was because I was doing require_tree . 我遇到了这个问题,那是因为我在做require_tree。 in my application.js file and I had precompiled my assets. 在我的application.js文件中,我已经预编译了我的资产。

Check that you don't have a public/assets file if you're going to be using require_tree . 如果您要使用require_tree ,请检查您是否没有公共/资产文件 in your application.js file. 在您的application.js文件中。

If you are using requirejs, check that your script doesn't include itself - just add console.error() call at start and see how many times it comes up. 如果您正在使用requirejs,请检查您的脚本是否包含自身 - 只需在start时添加console.error()调用并查看它出现的次数。 I had it come up because I copied optimization configuration to linear execution. 我得到了它,因为我将优化配置复制到线性执行。

Try this: 尝试这个:

   articleApp = new ArticleApp();
   Backbone.history = Backbone.history || new Backbone.History({});
   Backbone.history.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM