我对骨干很陌生,我正在尝试构建一个简单的应用程序。这是我到目前为止所拥有的
var Election = Backbone.Model.extend();
var Elections = Backbone.Collection.extend({
model: Election,
url: '/assets/data.json',
initialize: function() {
console.log('init col');
this.render();
return this;
},
render: function() {
console.log('rendering the collection');
return this;
},
// return this
});
var router = Backbone.Router.extend({
routes: {
'': 'root'
},
root: function(){
var collection = new Elections();
collection.fetch();
console.log(collection.length); //returns 0
}
});
var r = new router();
Backbone.history.start();
日志是这个
> init col
> rendering the collection
> 0
但是当我在控制台中手动创建一个新集合时,它会显示正确的长度和所有内容,我认为由于某种原因路由器调用发生得太早,但不确定。这是一个样本data.json
[
{
"year": 1868,
...
},
{
"year": 1872,
...
},