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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:20:19+00:00 2026-05-27T01:20:19+00:00

I am building a small Backbone.js application and added some custom getters to one

  • 0

I am building a small Backbone.js application and added some custom getters to one of the models (the name getter returns a concatenated first- and last name for example):

PersonModel = Backbone.Model.extend({
  get: function (attr) {
    if (typeof this[attr] == 'function') {
      return this[attr]();
    }
    return Backbone.Model.prototype.get.call(this, attr);
  },

  name: function() {
    return firstName + " " + lastName;
  }
})

I can now use person.get("name") to retrieve the name, nice. However, when I call toJSON on the the model these values aren’t included (and I suppose that makes sense). Problem is I use this to render my views:

this.template({people: this.collection.toJSON()});

What’s the best way to do this in Backbone.js? Manually creating the JSON with the overwritten getters?

Thanks!

  • 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-27T01:20:20+00:00Added an answer on May 27, 2026 at 1:20 am

    You could provide your own toJSON method on PersonModel:

    toJSON: function() {
        var attr = Backbone.Model.prototype.toJSON.call(this);
        attr.name = this.name();
        return attr;
    }
    

    The collection toJSON just calls toJSON on each model:

    // The JSON representation of a Collection is an array of the
    // models' attributes.
    toJSON : function() {
      return this.map(function(model){ return model.toJSON(); });
    },
    

    so adding your own toJSON to your model should work.

    You could also add name as a real attribute and then adjust your model’s validate method to update name if firstName or lastName changes and to ignore any direct attempts to change name or complain about “an attempt to edit a read-only attribute” when someone tries to pass a name change to set. There’s nothing that says that validate can’t change the attribute object that it is given so you could be given {firstName: 'x'} and change it to {firstName: 'x', name: 'x ' + this.get('lastName')} before validate returns. This would be a bit of an abuse of validate but there is no explicit prohibition against validate altering the attribute set and it is the only hook you have. I suppose you could have the model listen to change events on its own firstName and lastName and then trigger a set({name: ...}) but then you could have event ordering problems if someone else is watching only the first and last names.

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

Sidebar

Related Questions

I'm building small one page application with rails 3.1 mongodb and backbonejs. I have
Im building a small application and I have some click events binded to some
I am building some small Java application to run on Goole AppEngine. It is
I'm currently building a small web application that includes a fair amount of JavaScript.
I'm building a small web app in PHP that stores some information in a
I building a small application uploading documents on Google Docs. Is there any API
I'm building a small application in ASP.NET MVC. I'm still trying to find my
I'm building a small Stack Overflow application, but to collect information from Stack Overflow
I'm building a small application that will crawl sites where the content is growing
I'm building a small WPF application. For the layout in the application, i use

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.