我是主干和 Rails 的新手。当我返回最后一行的索引视图时,索引视图不会使用创建的新值进行更新。
class App.Views.ProfilesIndex extends Backbone.View
template: JST['profiles/index']
initialize: ->
@collection.on('reset', @render, this)
render: ->
$(@el).html(@template(profiles: @collection))
this
这是我的新视图代码
class App.Views.ProfilesNew extends Backbone.View
template: JST['profiles/new']
initialize: ->
@collection = new App.Collections.Profiles()
events: ->
'submit #new_profile': 'createProfile'
render: ->
$(@el).html(@template())
this
createProfile: (event) ->
event.preventDefault()
attributes = name: $('#new_profile_name').val()
@collection.create attributes,
success: -> Backbone.history.navigate("/profiles", {trigger: true})
因此,我需要在创建新元素并将其返回到索引视图时更新集合。
路由器
class App.Routers.Profiles extends Backbone.Router
routes:
'profiles': 'index'
'profiles/new': 'new'
initialize: ->
@collection = new App.Collections.Profiles()
@collection.fetch()
index: ->
view = new App.Views.ProfilesIndex(collection: @collection)
$('#container').html(view.render().el)
new: ->
view = new App.Views.ProfilesNew()
$('#container').html(view.render().el)