4

Given the following RelationalModel model:

var Server = Backbone.RelationalModel.extend({
    relations: [{
        type: Backbone.HasMany,
        key: 'databases',
        relatedModel: 'Database',
        collectionType: 'DatabaseCollection',
        includeInJSON: 'id'
    }],

    defaults: {
        databases: []
    },
});

var Database = Backbone.RelationalModel.extend({});
var DatabaseCollection = Backbone.Collection.extend({
    model: Database
});

And these objects:

new Database({
    id: 1,
    name: 'DB1'
});

new Database({
    id: 2,
    name: 'DB2'
});

var s1 = new Server({
    id: 3,
    name: 'S1',
    databases: [1,2]
});

What would be the easiest/recommended way to serialize/deserialize this model to something aproaching this JSON structure?:

{
    databases: [
        { id: 1, name: 'DB1' }
        { id: 2, name: 'DB2' }
    ],

    servers: [
        { id: 3, name: 'S1', databases: [1, 2] }                 
    ]
}

Such that the data can be sent to / read from the server in a single request.

Thanks!

Tim

Prasanth A R
  • 4,046
  • 9
  • 48
  • 76

1 Answers1

1

I was able to produce the JSON you described using your example with some minor changes in this fiddle I just created Example.

I made these changes due to some warnings that were being shown in the debugger and to get the result you described. Hope this helps.

  1. Moved the declaration of Database Model and DatabaseCollection to top before Server since Servers relatedModel and CollectionType point to those Models.
  2. For relatedModel and collectionType instead of using Strings used the reference to Database and DatabaseCollection
  3. Created a collection for Servers called ServerCollection
  4. Added a few more examples

Here is the code you end up with, I just created a plain old Backbone model to combine the two collection into one. Calling toJSON gives you the single JSON object to transmit to the server.

var Database = Backbone.RelationalModel.extend({});
var DatabaseCollection = Backbone.Collection.extend({
    model: Database
});

var Server = Backbone.RelationalModel.extend({
    relations: [{
        type: Backbone.HasMany,
        key: 'databases',
        relatedModel: Database,
        collectionType: DatabaseCollection, 
        includeInJSON: 'id' 
    }],

    defaults: {
        databases: []
    } 
});
var ServerCollection = Backbone.Collection.extend({
    model: Server 
});

var allDatabases = new DatabaseCollection(); 
allDatabases.add([
    new Database({ id: 1, name: 'DB1' }),  
    new Database({ id: 2, name: 'DB2' }),
    new Database({ id: 3, name: 'DB3' }),
    new Database({ id: 4, name: 'DB4' })
]);   

var allServers = new ServerCollection(); 
allServers.add([
    new Server({
        id: 30,
        name: 'S1', 
        databases: [
          allDatabases.get(1),
          allDatabases.get(2)
        ]
    }),
    new Server({
        id: 40,
        name: 'S2',
        databases: [
          allDatabases.get(3),
          allDatabases.get(4)
        ]
    })
]);  

// combine into an object to transfer to server as one 
var serverObject = new Backbone.Model({
    'servers': allServers,
    'databases': allDatabases
});  
console.log(serverObject.toJSON()); 
Scott
  • 729
  • 1
  • 11
  • 30