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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:24:14+00:00 2026-06-10T22:24:14+00:00

Questions: What is the proper way to initialize a backbone.js model when there are

  • 0

Questions: What is the proper way to initialize a backbone.js model when there are attributes that need to be stored in specific ways? Do i need to map the attributes that don’t need any special formatting? I thought backbone.js did some kind of auto-mapping.

Example:

var MyModel = Backbone.Model.extend({

    initialize: function (options) {

        // These attributes need to be stored in a different format
        // Dates
        this.startYear = new Date(options.startTime).getFullYear();
        // Rounding numbers
        this.wholeNumber = Math.Round(options.numberWithDecimals);
        // Storing empty strings as nulls
        if (options.fullName == null || options.fullName == "") {
            this.fullName == null;
        } else {
            this.fullName = options.fullName;
        }

        // These are fine as they are
        this.fieldA = options.fieldA;
        this.fieldB = options.fieldB;
        this.fieldC = options.fieldC;
    },
});
  • 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-10T22:24:15+00:00Added an answer on June 10, 2026 at 10:24 pm

    First you have to differentiate between attributes and instance variables.

    Attributes: IMHO, it should be plain objects as String or Integer. They travel around the client and the server through the REST API. They are manipulated through the Model.get()/Model.set() methods. They are sent to the server through the Model.toJSON() (also they use to be sent to the template using the same .toJSON() method. If they change in some way then Backbone events are triggered. You can customize the initialization of this attributes manipulating the server side JSON information before it is sent to the Model overriding the Model.parse() method as @muistooshort has suggested.

    Instance Variables: (the this.myAttribute thing) They can be complex objects. The don’t trigger any implicit event in their change and they are not sent to the server in the save and update calls, and, in an standard way, they are not sent to the template.

    In your example you’re not storing any complex object and if you are not affraid that your model is gonna send more attributes to the server than it receives from the server you can go for the @muistooshort suggestion:

    // code no tested
    var MyModel = Backbone.Model.extend({
      parse: function(resp, xhr) {
        resp.startYear = new Date( resp.startTime ).getFullYear();
        resp.wholeNumber = Math.Round( resp.numberWithDecimals );
        if( resp.fullName == "" ) resp.fullName == null;
    
        return resp;
      },
    });
    

    Just remember you these are attributes and you have to access them in this way my_model.get( "startYear" )

    The only problem with this solution is the derived attributes won’t be updated if the original attribute changes. So you can come with another implementation:

    // code no tested
    var MyModel = Backbone.Model.extend({
      initialize: function(){
        this.updateAttributes();
        this.on( "change", this.updateAttributes, this );
      },
    
      updateAttributes: function() {
        this.set( "startYear", new Date( this.get( "startTime" ) ).getFullYear() );
        this.set( "wholeNumber", Math.Round( this.get( "numberWithDecimals" ) ) );
        if( this.get( "fullName" ) == "" ) this.set( "fullName", null );
      },
    });
    

    Updated

    As @TomTu has suggested if your onlive attributes are only needed to feed the templates then a decorator is the best solution: https://stackoverflow.com/a/9687672/316700

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

Sidebar

Related Questions

I have a question about the proper, best way to manage the model. I
There have been several questions over the past few days about the proper use
I think I don’t quite get the idea behind the proper usage of Backbone
I have two questions for you guys: Which is the proper way ot word-wrap
I have a question regarding the proper way to modify a php DateTime object.
QUESTION: What is the proper way to use .get() in conjunction with .one() (or
Just a quick question that I can't seem to find a proper answer to
I'm unsure what the proper way is to access parts of the requested URL.
There's a couple questions here on StackOverflow on the subject of threading with the
I am trying to figure out the proper way to use NSManagedObjectContexts when you

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.