Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:04:42+00:00 2026-05-27T18:04:42+00:00

I have a collection of Comments and a view which is used to create

  • 0

I have a collection of Comments and a view which is used to create new comments. Each comment has some client side validation going on:

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

The Comments collection is super simple:

class Designer.Collections.Comments extends Backbone.Collection
  model: Designer.Models.Comment

I create comments in the NewComment view. This view has access to the comments collection and uses it to create new comments. However, validations fails in the Comment model don’t seem to bubble up through the collection. Is there a batter way to do this?

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:04:43+00:00Added an answer on May 27, 2026 at 6:04 pm

    The problem is that the collection event that aggregates all of the model events hasn’t been hooked up yet. That hookup happens in the _add() function. Since the validation fails before the model gets added, you don’t get the event.

    The only indication of failure happens when create returns false but it looks like you’ve figured that out already.

    If you need the validation errors, you will need to come up with a way to get the errors to you.

    One way would be to fire off an EventAggregator message inside the validator. The other would be to circumvent or re-define the Collection.create function to hook the error event on the model.

    Something like this?

    model = new Designer.Models.Comment()
    model.bind "error", @handleError
    if model.set body: $newComment.val()
        model.save success: -> @collection.add(model)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have entity Recipes and it has a HasMany collection comments. In a MVC
I have a teacher rating system. When I got to create a new comment
Has anyone run into this problem? I have a collection of comments that I
I have Collection List<Car> . How to compare each item from this collection with
I'm planning to have collection of items stored in a TCollection. Each item will
I have Class and Student objects. Both have collection of another as property. Which
I have a Add view which uploads file which is element of Question class
I'm trying to create a site with news that can have comments. I've been
I have a Rails application with several models-views-controllers which have some similar characteristics, for
A user can have many posts, and each post can have many comments. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.