Search code examples
jsonbackbone.jscontent-typedataformat

Define Backbone model for content-type "application/x-www-form-urlencoded"


I am having some issues in assigning the data to the model. Data is coming is in the foll format.

"Address" : [{
   "id" : "1"
   "name" : "abc"
   "phone" : "9876543210",
   "email" : "abc@example.com"
},{
   "id" : "2"
   "name" : "XYZ"
   "phone" : "9872543210",
   "email" : "XYZ@example.com"
}] 

But the model i have created for above looks like the foll :

 var AddressBookModel= Backbone.Model.extend({       
        defaults: {
            name: "",
            phone: "",
            email: ""
        },
        idAttribute: "id"
    }); 

I Know this is not the correct model created for the data. What is the correct way to define the model for above format?


Solution

  • If your are looking to "parse away" the Address object and return an array in your collection try this parse method:

    var c = Backbone.Collection.extend({
      parse: function(data){
        return data.Address;
      }
    });