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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:19:35+00:00 2026-06-13T13:19:35+00:00

I’m seeing strange behaviour using JSON.stringify against a subclassed model in Spine, and I’m

  • 0

I’m seeing strange behaviour using JSON.stringify against a subclassed model in Spine, and I’m hoping someone can help!

Here’s a simplified excerpt from some code that we’ve got on one of our projects:

define([
    "jquery",
    "underscore"
],

function ($, _) {
    var SuperClass = Spine.Model.sub();
    SuperClass.configure("SuperClass", "SuperClassProperty");

    var SubClass = SuperClass.sub();
    SubClass.configure("SubClass", "SubClassProperty");

    var instance = new SubClass({ SuperClassProperty: "Super", SubClassProperty: "Sub" });
    console.log(instance);

    var json = JSON.stringify(instance);
    console.log(json);
});

The “console.log(instance)” is printing out exactly what I would expect in this scenario:

result
    SubClassProperty: "Sub"
    SuperClassProperty: "Super"
    cid: "c-0"
    __proto__: ctor

However, when I use JSON.stringify against the instance, this is all that I am returned:

{"SubClassProperty":"Sub"} 

Why doesn’t the SuperClassProperty get included in the stringify?

I’ve ruled out a problem with the JSON.stringify method by forcing JSON2 to override Chrome’s native JSON object; both implementations yield the same result. It looks like stringify will delegate to the “toJSON” function on the object if there is one – and in this case there is (as part of Spine).

So it looks like either (a) this is a bug in Spine, or (b) I’m doing something incorrectly, which is the more likely option.

I know I can work around this problem by re-configuring the superclass properties on the subclass as well:

SubClass.configure("SubClass", "SuperClassProperty", "SubClassProperty");

However this seems counter-intuitive to me (what’s the point of subclassing?), so I’m hoping that’s not the answer.

Update: I’ve done some debugging through the Spine source code, and from what I can tell the problem is the way that I’m configuring the subclass:

    var SubClass = SuperClass.sub();
    SubClass.configure("SubClass", "SubClassProperty");

Calling “configure” here appears to wipe out the attributes from SuperClass. The “toJSON” implementation on the Model prototype is as follows:

Model.prototype.toJSON = function() {
    return this.attributes();
};

Since the attributes collection is reset when SubClass is configured, the SuperClass properties don’t come through in the JSON string.

I’m not sure if I shouldn’t be calling “configure” on subclassed objects, but I can’t find anywhere in the documentation that says I should be doing something else – this is the only reference I can find for subclassing Models (from: http://spinejs.com/docs/models):

Models can be also be easily subclassed:

var User = Contact.sub();
User.configure("User");
  • 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-13T13:19:36+00:00Added an answer on June 13, 2026 at 1:19 pm

    As I suspected, the problem was in the way that I’m using Spine. This comment from the author of Spine infers that using “configure” on a subclass will wipe out the attributes of the superclass. I have to admit I don’t understand why this is; it seems counter-intuitive to me, but at least I now know that it’s not a bug.

    In case anyone else runs into this issue, the way I’ve worked around it is by adding my own extension method to the Spine Model as follows:

    (function () {
        var Model = Spine.Model;
    
        Model.configureSub = function () {
            var baseAttributes = this.attributes.slice();
            this.configure.apply(this, arguments);
    
            this.attributes = baseAttributes.concat(this.attributes);
            return this;
        };
    })();
    

    Now to configure my subclass:

    var SubClass = SuperClass.sub();
    SubClass.configureSub("SubClass", "SubClassProperty");
    

    And now my JSON correctly reflects the properties from both the super and subclasses.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Does anyone know how can I replace this 2 symbol below from the string

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.