eri*_*bae 8 javascript backbone.js
我在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);
Run Code Online (Sandbox Code Playgroud)
该应用程序本身似乎工作正常.但Chrome中的错误是神秘的.
12/30/2013更新:看起来这可能是一个常见的问题,我决定大胆的原因.
我遇到了同样的问题并解决了它.原因是我
imported the js file twice
因此,我建议您检查包含此脚本的页面,看看它是否已经两次引入.
小智 -3
尝试这个:
articleApp = new ArticleApp();
Backbone.history = Backbone.history || new Backbone.History({});
Backbone.history.start();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8659 次 |
| 最近记录: |