[英]Backbone.history has already been started
我在Backbone.js應用程序上收到錯誤“Backbone.history已經啟動”。 這是我的代碼。
(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);
該應用程序本身似乎工作正常。 但Chrome中的錯誤是神秘的。
12/30/2013更新:看起來這可能是一個常見的問題,我決定大膽的原因。
我遇到了同樣的問題並解決了它。 原因是我
imported the js file twice
因此,我建議您檢查包含此腳本的頁面,看看它是否已經兩次引入。
Yujings說對了! 它導入了兩次js。 更新我的“修復”(肯定更像是一種解決方法)是將其置於路由器中。
Backbone.history.stop();
Backbone.history.start();
可能不是最理想的,但它以天真的方式完成了工作。
由於我的Rails 4應用程序中的Turbolinks,我遇到了同樣的問題。 這種解決方法幫助了我。 也許它也會對你有所幫助:
initialize: ->
new MyApp.Routers.Posts
Backbone.history.start() unless Backbone.History.started
$(document).on "page:change", ->
Backbone.history.stop()
Backbone.history.start()
在這里找到這個答案: https : //github.com/codebrew/backbone-rails/issues/134
你的代碼似乎還可以。 你確定你加載了一次嗎? 如果你把history.log()放在history.start()之前,那么日志是否只打印一次?
通常,當多次調用history.start()時會出現此錯誤。 我擔心你需要找到第二個電話。 您在此處發布的代碼不足以找到它。
我遇到了這個問題,那是因為我在做require_tree。 在我的application.js文件中,我已經預編譯了我的資產。
如果您要使用require_tree ,請檢查您是否沒有公共/資產文件。 在您的application.js文件中。
如果您正在使用requirejs,請檢查您的腳本是否包含自身 - 只需在start時添加console.error()調用並查看它出現的次數。 我得到了它,因為我將優化配置復制到線性執行。
嘗試這個:
articleApp = new ArticleApp();
Backbone.history = Backbone.history || new Backbone.History({});
Backbone.history.start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.