theJava
theJava

Reputation: 15034

Forming JSON requests in backbone.js

Here is the JSON Object which i need to form.

{
    "Header1": {
        "Login": {
            "SiteId": "",
            "UserName": "",
            "UserPassword": "",
            "UserAlias": ""
        },
        "Credential": {
            "Login": "",
            "Password": ""
        }
    },
    "Header2": {
        "DestinationID": "",
        "UserID": "",
        "SourceID": ""
    }
}

On the click of login, i need to form this JSON and send to my service using backbone.js. I am just confused on where to form this in backbone.js

var Client = Backbone.Model.extend({
    defaults: {

    }
});

Should i add my JSON Object to defaults and use them?

Upvotes: 0

Views: 218

Answers (1)

chestermano
chestermano

Reputation: 853

The backbone model usually relates to a model or db table on the server side. With this in mind you can use @model.set(attributes) to set the value in the model and then use @model.save to send to the server. If you are storing objects on your server model just define them in backbone before setting in the model.

@model = new Client()

new_object = new Object()
new_object.site_id = ""
new_object.UserName = ""
etc..

@model.set(
  Header1: new_object,
  Header2: somethingelse
)

@model.save()

If this is not the case and the model doesn't correspond to a model or table on the server you might be better off just using JQuery Ajax call and manually construct the JSON you need as above. Hope this helps.

Upvotes: 2

Related Questions