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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:38:23+00:00 2026-06-03T14:38:23+00:00

Is there a standard way to deal with non-saveable values in Backbone. e.g. MyModel

  • 0

Is there a standard way to deal with non-saveable values in Backbone.

e.g.

MyModel = Backbone.extend(Backbone.Model, {
    initialize: function () {
        this.set({'inches': this.get('mm') / 25});
    }
})

If I call save() on this model it will throw an error as there is no corresponding database field for inches. I can think of a few ways to fix this, but am wondering if there’s a tried and tested approach generally best used for this?

At the moment my preferred solution is to extend Backbone’s toJSON method and to allow passing of a boolean parameter dontCleanup to allow for it to still return all the model’s values (including the non saveable ones) when it’s needed e.g. for passing to a template.

  • 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-03T14:38:26+00:00Added an answer on June 3, 2026 at 2:38 pm

    I like Peter Lyon’s idea. I’ve thought about that a few times, but never actually put it in place. For all the ways that I have handled this, though, here are my two favorites:

    • Non-“attribute” values
    • View Models

    Non-Attribute Values

    This one is simple: don’t store the values you need in the model’s standard attributes. Instead, attach it directly to the object:

    
    myModel.someValue = "some value";
    

    The big problem here is that you don’t get all of the events associated with calling set on the model. So I tend to wrap this up in a method that does everything for me. For example, a common method I put on models is select to say that this model has been selected:

    
    MyModel = Backbone.Model.extend({
      select: function(){
        if (!this.selected){
          this.selected = true;
          this.trigger("change:selected", this, this.selected);
        }
      }
    });
    

    In your case, I’m not sure this would be a good approach. You have data that needs to be calculated based on the values that are in your attributes already.

    For that, I tend to use view models.

    View models.

    The basic idea is that you create a backbone model that is persist-able, as you normally would. But the you come along and create another model that inherits from your original one and adds all the data that you need.

    There are a very large number of ways that you can do this. Here’s what might be a very simple version:

    
    MyModel = Backbone.Model.Extend({ ... });
    
    MyViewModel = function(model){
      var viewModel = Object.create(model);
    
      viewModel.toJSON = function(){
        var json = model.toJSON();
        json.inches = json.mm / 25;
        return json;
      };
    
      return viewModel;
    });
    

    The big benefit of wrapping this with Object.create is that you now have a prototypal inheritance situation, so all of your standard functionality from the model is still in place. We’ve just overridden the toJSON method on the view model, so that it returns the JSON object with the inches attribute.

    Then in a view that needs this, you would wrap your model in the initialize function:

    
    MyView = Backbone.View.extend({
      initialize: function(){
        this.model = MyViewModel(this.model);
      },

    render: function(){
    var data = this.model.toJSON(); // returns with inches
    }
    });

    You could call new MyViewModel(this.model) if you want, but that's not going to do anything different, in the end, because we're explicitly returning an object instance from the MyViewModel function.

    When your view's render method calls toJSON, you'll get the inches attribute with it.

    Of course, there are some potential memory concerns and performance concerns with this implementation, but those can be solved easily with some better code for the view model. This quick and dirty example should get you down the path, though.

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

Sidebar

Related Questions

IOW, is there a standard way of testing for this, a la: if (!
This is a weird question, but is there a standard way to manipulate the
Is there any standard way of debugging Javascript on a webpage that's being accessed
is there a standard way to encode the user location (country, state/region, town), so
Is there a standard way of maintaining a weak pointer to a parent (which
Is there a standard way / C# library to convert a string into a
Is there a standard way to access the underlying container of stack , queue
Is there any standard way to route all Key events from the control A
Is there any standard way of implementing some sort of a write-through buffer for
Is there a standard way, now that the MySQL migration toolkit is no longer

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.