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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:50:53+00:00 2026-06-13T01:50:53+00:00

I’m working on a project where the request and response payloads are all namespaced.

  • 0

I’m working on a project where the request and response payloads are all namespaced. For example:

{
    'SourceMachine':{
        'Host':'some value',
        'Description':'some value',
        'UserName':'some value',
        'Password':'some value'
    }
}

To be able to do get() and set() on the individual fields, I overrode the parse method in my “source_model” like so:

parse : function(response, xhr) {
    return response.SourceMachine;
}

That’s all well and good. But the issue is this: When I want to do a POST or PUT operation, I have to get the Host, Description, UserName and Password attributes into the SourceMachine namespace. Basically what I’ve been doing is copying the model attributes to a temporary object, clearing out the model, then saving as follows:

var tempAttributes = this.model.attributes;
this.model.clear();
this.model.save({SourceMachine: tempAttributes});

This works, but it screams of KLUGE! There’s got to be a better way of working with namespaced data. Thanks!

Update

I’ve now created a require.js module for a base model that I’ll be using for all the models in my app, since they all rely on namespaced data:

define(function() {
    return Backbone.Model.extend({
        /**
         * Adding namespace checking at the constructor level as opposed to
         * initialize so that subclasses needn't invoke the upstream initialize.
         * @param attributes
         * @param options
         */
        constructor : function(attributes, options) {
            //Need to account for when a model is instantiated with
            //no attributes. In this case, we have to take namespace from
            //attributes.
            this._namespace = options.namespace || attributes.namespace;

            //invoke the default constructor so the model goes through
            //its normal Backbone setup and initialize() is invoked as normal.
            Backbone.Model.apply(this, arguments);
        },
        /**
         * This parse override checks to see if a namespace was provided, and if so,
         * it will strip it out and return response[this._namespace
         * @param response
         * @param xhr
         * @return {*}
         */
        parse : function(response, xhr) {
            //If a namespace is defined you have to make sure that
            //it exists in the response; otherwise, an error will be
            //thrown.
            return (this._namespace && response[this._namespace]) ? response[this._namespace] 
                                                                  : response;
        },
        /**
         * In overriding toJSON, we check to see if a namespace was defined. If so,
         * then create a namespace node and assign the attributes to it. Otherwise,
         * just call the "super" toJSON.
         * @return {Object}
         */
        toJSON : function() {
            var respObj = {};
            var attr = Backbone.Model.prototype.toJSON.apply(this);
            if (this._namespace) {
                respObj[this._namespace] = attr;
            } else {
                respObj = attr;
            }
            return respObj;
        }
    })
});
  • 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-13T01:50:54+00:00Added an answer on June 13, 2026 at 1:50 am

    Create and update operations on models call toJSON to produce the data for the server:

    toJSON model.toJSON()

    Return a copy of the model’s attributes for JSON stringification. This can be used for persistence, serialization, or for augmentation before being handed off to a view.

    You could provide your own toJSON:

    toJSON: function() {
        return { SourceMachine: _(this.attributes).clone() };
    }
    

    That should make your server happy. Unfortunately, toJSON is also commonly used to provide data for your templates and you probably don’t want the SourceMachine noise in there; if so, then add another method to prepare data for your templates:

    // Or whatever you want to call it...
    valuesForTemplate: function() {
        return _(this.attributes).clone();
    }
    

    That’s what the standard toJSON method does internally.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: 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.