0
votes

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.

1

1 Answers

0
votes

I think this definitely has to do with the idAttribute property you have set in your Location model. You have id as 3 in you json_data but you have set idAttribute to _id, so what might be happening is, since json_data doesn't have _id it is undefined and it is then set to your model's id so model's id becomes undefined. Now, since id is undefined on saving model, it will treat it as a new model and make a POST request.

From the doc:

idAttribute model.idAttribute

A model's unique identifier is stored under the id attribute. If you're directly communicating with a backend (CouchDB, MongoDB) that uses a different unique key, you may set a Model's idAttribute to transparently map from that key to id.

FIX:

Either you remove the idAttribute you have set to _id or add a property _id to your json_data.