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 7765487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:10:42+00:00 2026-06-01T15:10:42+00:00

I want to have a Backbone model with float attributes in it but without

  • 0

I want to have a Backbone model with float attributes in it but without worrying too much about variable types.

I would like to encapsulate the value parsing right there in the model so I am thinking of overriding the set function:

var Place = Backbone.Model.extend({
  set: function(attributes, options) {
    if (!_.isEmpty(attributes.latitude)){
      attributes.latitude == parseFloat(attributes.latitude);
    }
    if (!_.isEmpty(attributes.longitude)){
      attributes.longitude == parseFloat(attributes.longitude);
    }
    Backbone.Model.prototype.set.call(this, attributes, options);
  }
});

However this seems cumbersome, since I would have a similar logic in the validate method and potentially repeated across multiple models. I don’t think the View should take care of these conversions.

So what is the best way of doing it?

  • 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-01T15:10:43+00:00Added an answer on June 1, 2026 at 3:10 pm

    Use a validation plugin for your model so that you can validate the input in a generic fashion.

    There are several out there including one that I have written:

    • Backbone.Validator
    • Backbone.Validation

    Then you don’t worry about performing data validation anywhere else – your model does it and sends out and error message you can listen for and provide appropriate feedback.

    Also, a lat/lng pair can, in rare circumstances, be an integer, such as Greenwich England: 0,0 or the north pole: 90,180. And since JavaScript only has “number” any valid input for parseFloat is also valid for parseInt.

    But parseFloat will always return a float.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I have a view without a model? I just want to render some
I want to override backbone.sync i have already asked this but the problem is
I have Backbone.js collection that holds (for example) 30 items. I want to pass
I have a backbone model which contains an array which I am updating with
Lets have the following code : ( function($) { TeacherModel = Backbone.Model.extend({ defaults :
I want to do: var MyModel = Backbone.model.extend({ someProp: { ... }, . .
Basically, I want my model to have its own validation logic while having the
I have a backbone collection and when I remove a model from the collection,
I have this: var SomeNamespace = { Model: Backbone.Model.extend(), View: Backbone.View.extend({ model: new this.Model,
I really like Backbone, but I am having the hardest time doing what would

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.