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

  • Home
  • SEARCH
  • 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 8623041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:11:30+00:00 2026-06-12T07:11:30+00:00

For some strange reason my model is still setting attributes when I return a

  • 0

For some strange reason my model is still setting attributes when I return a string in the models validation. Here is my code validation code:

Model = Backbone.Model.extend({
    validate: function( attributes ){
        var tax = attributes.tax;

        if(tax.amount < 0.0 || typeof tax.amount !== "number"){
            return "The tax amount cannot be negative and must be a number.";
        }
    },
   defaults: {
       "tax": {
            "amount": 100
       }
   },
   setTax: function(amount){
      var tax = this.get("tax");
      tax.amount = amount;
      this.set("tax", tax);
   }
})

I then have the model listen for the error event and console log it through:

    model = new Model();

    View = Backbone.View.extend({
        initialize: function(){
            this.model.on('error', function(model, error){
                console.log("ERROR: " + error);
            })
        }
    });
    view = new View({model: model});
    view.model.setTax(-100);

The console log is being printed, but for some reason the model is still setting the property. Is there anything I need to return to not have the model set the property? According to the Backbone.js docks if you return anything from the validation then it is suppose to not set the property. I’m using Backbone.js version 0.9.2

  • 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-06-12T07:11:31+00:00Added an answer on June 12, 2026 at 7:11 am

    Your problem is right here:

    var tax = this.get("tax");
    

    The get method basically does return this.attributes[attr] and the tax attribute is an object, the result is that your var tax and the model’s this.attributes.tax are the same thing. So, when you say tax.amount = amount, you’re actually directly editing the tax that is inside the model’s attributes.

    When working with mutable attributes (i.e. anything other than numbers, strings, and booleans), you need to make copies before changing them:

    setTax: function(amount) {
        var tax = _(this.get("tax")).clone();
        tax.amount = amount;
        this.set("tax", tax);
    }
    

    Making a copy also prevents Backbone from thinking that your set call isn’t actually doing anything. If you don’t clone the tax as above, you won’t trigger the change event on the model and that’s probably not what you want.

    A few examples (open your console please):

    • Invalid tax amount with a _.clone in setTax.
    • Valid tax amount with a _.clone and a change handler.
    • Valid tax amount without _.clone and with a change handler.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered a strange behaviour of the model validation in Backbone.js. When a
well for some strange reason IE gives me and InvalidAuthenticityToken error almost every time
I'm passing two strings between two activities and for some strange reason, the strings
For some reason, the code below fails on the second line with run-time error
For some strange reason, when trying to access the last 100 records of a
For some strange reason, a five year old internal asp.net web app that is
For some strange reason, the \n and \r escape sequences do not seem to
For some strange reason I am unable to add the canonical tag to opencart
For some strange reason, my text-align: center; isn't working. It's weird 'cause it's the
While I was writing some Scala code, I got a strange error message 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.