嗨,我有一个新的主干,我似乎无法让它在我的 Rails 应用程序中工作。
我正在尝试在我的“帖子/新”页面中为集合创建一个模型。
posts_new.js.coffee
class Poster.Views.PostsNew extends Backbone.View
template: JST['posts/new']
events:
'submit #new_post': 'createPost'
render: ->
$(@el).html(@template(post: "NEW PAGE"))
this
createPost: (event) ->
event.preventDefault()
if $('#new_post_title').val() == '' || $('#new_post_message').val() == ''
alert "Please fill out form"
$('#new_post')[0].reset()
else @collection.create(title: $('#new_post_title').val(), message: $('#new_post_message').val()); $('#new_post')[0].reset(); Backbone.history.navigate("/posts", true)
这是我的posts_routes.js.coffee,我在其中传递@collection 变量
class Poster.Routers.Posts extends Backbone.Router
routes:
'posts':'index'
'':'index'
'posts/new': 'new'
'posts/:id': 'show'
initialize: ->
@collection = new Poster.Collections.Posts()
@collection.fetch({reset: true})
index: ->
view = new Poster.Views.PostsIndex(collection: @collection)
$('#index_container').html(view.render().el)
show: (id) ->
view = new Poster.Views.PostsShow()
$('#index_container').html(view.render().el)
new: ->
view = new Poster.Views.PostsNew(collection: @collection)
$('#index_container').html(view.render().el)