У меня есть коллекция комментариев и представление, которое используется для создания новых комментариев. Каждый комментарий имеет некоторые клиентские проверки на стороне происходит:Привязать к событию ошибки модели, созданной методом 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