My backbone Model:
var Location = Backbone.Model.extend({
idAttribute: "_id",
// Default attributes ensure that each todo created has `title` and `completed` keys.
defaults: {
nick_name: null, //Example: The Office
street_address: '214 West 29th Street',
extended_address: null, // Example: Suite 205
cross_street: null,
locality: 'New York',
region: 'NY',
postal_code: 10001,
country: 'United States',
},
initialize: function() {
}
});
My backbone collection:
var LocationCollection = Backbone.Collection.extend({
// Reference to this collection's model.
model: Location
});
I have a global variable that is a JSON array of models:
json_data = [{country: "United States of America",
cross_street: "",
extended_address: "",
id: 3,
locality: "Brooklyn",
nick_name: "",
postal_code: 11208,
region: "NY",
street_address: "3039 Fulton street"}]
When i try to add the array of model attributes to the Collection the models are always treated as new Models not as saved to the database. Thus, I try to call save on any of the models and I get a POST request instead of a PUT request. Also, i've tried just create the Models with no Collection, using the model set method, and the id attribute is never set to the Model id even though I can call the model_instance.attributes method and I see that id is part of the output.
this.locationCollection = new LocationCollection();
this.locationCollection.url = urlRoot;
this.locationCollection.add(json_data);
this.locationCollection.each(function(model, index, option) {
console.log(model.id);
console.log(model.urlRoot)
console.log(model.url)
console.log(model.attributes);
}, this)
Output from console logging:
undefined
undefined
some function for url
{country: "United States of America",
cross_street: "",
extended_address: "",
id: 3,
locality: "Brooklyn",
nick_name: "",
postal_code: 11208,
region: "NY",
street_address: "3039 Fulton street"} // correct expected output for model.attributes
Please tell me if I'm adding models to a collection correctly.