以前,我的骨干路由器是这样的:骨干+ Rails的采集获取
class App.Routers.ThingsRouter extends Backbone.Router
routes: '': 'index'
routes: 'previews/:id': 'show'
initialize: ->
@collection = new App.Collections.ThingsCollection
@collection.fetch
index: ->
view = new App.Views.ThingsIndex(collection: @collection)
$('#app-container').html(view.render().el)
show: (id) ->
@model = @collection.get(id)
view = new App.Views.ThingsShow(model: @model)
$('#app-container').html(view.render().el)
当导航到http://localhost
,我会得到index
视图渲染,以及对单个元素点击的时候,我会得到show
视图渲染。但是,如果我直接访问http://localhost/things/1
(即通过输入URL),则不会呈现show
视图。我意识到这是因为在@collection.fetch
完成之前正在呈现视图。我改变了我的路由器以下内容:
class App.Routers.ThingsRouter extends Backbone.Router
routes: '': 'index'
routes: 'previews/:id': 'show'
initialize: ->
@collection = new App.Collections.ThingsCollection
index: ->
@collection.fetch success: =>
view = new App.Views.ThingsIndex(collection: @collection)
$('#app-container').html(view.render().el)
show: (id) ->
@collection.fetch success: =>
that.model = that.collection.get(id)
view = new App.Views.ThingsShow(model: @model)
$('#app-container').html(view.render().el)
这工作正常。但是,显然有一点延迟,因为每次切换路由时都会重新获取集合。这是很好的骨干练习吗?不知道是否有更好的方法来做到这一点。
哇,我完全一样的问题。我不认为我会找到正确的词组合来谷歌找到有同样问题的人,但我做到了! – tybro0103 2013-01-03 22:31:18