嗨,我有如下主干应用程序:
code.js.coffee
window.Code =
Models: {}
Collections: {}
Views: {}
Routers: {}
initialize: ->
new Code.Routers.Todos();
Backbone.history.start()
$(document).ready ->
Code.initialize()
todos_router.js.coffee
class Code.Routers.Todos extends Backbone.Router
routes:
'': 'index'
'todos/:id': 'show'
initialize: ->
@collection = new Code.Collections.Todos()
@collection.fetch()
index: ->
view = new Code.Views.TodosIndex(collection: @collection)
view.render()
$('#container').html(view.el)
show: (id)->
alert "#{id}"
todos.js.coffee --> 集合
class Code.Collections.Todos extends Backbone.Collection
url: '/todos'
todos_index.js.coffee
class Code.Views.TodosIndex extends Backbone.View
template: JST['todos/index']
initialize: ->
this.collection.on('reset',this.render,this)
render: ->
$(@el).html(@template(todo: this.collection))
现在的问题是当我在模板上呈现集合以获取长度时,即使数据库上有 1 条记录,它仍然给我结果 0。我在这里做错了什么?this.collection 的控制台输出如下
Todos {length: 0, models: Array[0], _byId: Object, constructor: function, url: "/todos"…}
_byId: Object
_events: Object
length: 1
models: Array[1]
__proto__: ctor
谢谢!