4
我有意见的集合,它是用来创建新的注释视图。每个注释有一些客户端验证回事:绑定到由collection.create()创建的模型的错误事件?
class Designer.Models.Comment extends Backbone.Model
validate: (attrs) ->
errors = []
# require presence of the body attribte
if _.isEmpty attrs.body
errors.push {"body":["can't be blank"]}
unless _.isEmpty errors
errors
的评论集是超级简单:
class Designer.Collections.Comments extends Backbone.Collection
model: Designer.Models.Comment
我创建的NewComment
查看评论。此视图可以访问评论集合,并将其用于create
新评论。然而,验证在Comment
模型没有不似乎通过收集泡沫了。有没有一种方法可以做到这一点?
class Designer.Views.NewComment extends Backbone.View
events:
'submit .new_comment' : 'handleSubmit'
initialize: ->
# this is where the problem is. I'm trying to bind to error events
# in the model created by the collection
@collection.bind 'error', @handleError
handleSubmit: (e) ->
e.preventDefault()
$newComment = this.$('#comment_body')
# this does fail (doesn't hit the server) if I try to create a comment with a blank 'body'
if @collection.create { body: $newComment.val() }
$newComment.val ''
this
# this never gets called
handleError: (model, errors) =>
console.log "Error registered", args