1

I am working on app at moment, when I create a group the save function works great the model gets added to the collection, and saved to the database, however if I then want to edit the group I have just made, when I hit save the new model is created (and POST request) instead of the data being edited and a PUT request being fired. Here is my save function - is there any reason I am not firing PUT requests when editing an exsisting model?

GroupModalHeaderView.prototype.save = function(e) {
  var $collection, $this;
  if (e) {
    e.preventDefault();
  }

  $this = this;
  if (this.$("#group-name").val() !== "") {
    $collection = this.collection;
    if (this.model.isNew()) {
      this.collection.add(this.model);
    }
    return this.model.save({ name: this.$("#group-name").val()}, {
      async: false,
      wait: true,
      success: function() {
        var modelID = $this.model.get('id');

        return this.modal = new app.GroupModalView({
          model: $this.collection.get(modelID),
          collection: $this.collection
        });
      }
    });
  }

};

This is my model defaults,

Group.prototype.defaults = {
  user_id: "",
  name: "New Group",
  email: "",
  url: "",
  telephone: "",
  mobile: "",
  fax: "",
  people: ""
};

Here is the console.log of this.model before it is saved,

    Group {cid: "c116", attributes: Object, _changing: false, _previousAttributes:    Object, changed: Object…}
        _changing: false
        _events: Object
        _pending: false
        _previousAttributes: Object
        email: ""
        fax: ""
        mobile: ""
        name: "New Group"
        people: ""
        telephone: ""
        url: ""
        user_id: ""
        wait: true
        __proto__: Object
        attributes: Object
        changed: Object
        cid: "c116"
        collection: GroupCollection
        id: 189
        __proto__: ctor
Udders
  • 67
  • 2
  • 9

1 Answers1

1

The reason Backbone.js is firing a POST request instead of PUT is because your model does not have a unique identifier id associated with it.If there is not id attribute associated with your model, Backbone.js will always fire a POST request to save a new entry to db.

From backbone's website :

save model.save([attributes], [options]) ... If the model isNew, the

save will be a "create" (HTTP POST), if the model already exists on the server, the save will be an "update" (HTTP PUT).

Read this SO question for more information.

Community
  • 1
  • 1
Nishant Jani
  • 1,965
  • 8
  • 30
  • 41
  • you will need to set the `id` in the model , before you dispatch a save request – Nishant Jani Jul 17 '13 at 11:06
  • But if i do console.log($this.model.get("id") in the success callback I can see the models ID? – Udders Jul 17 '13 at 11:17
  • alright, give me the console.log before you save the model to your db – Nishant Jani Jul 17 '13 at 11:19
  • if your model has an id before the save , it really not possible that backbone would dispatch a POST, because the model.isNew() would return false, what does model.isNew() return for you ? – Nishant Jani Jul 17 '13 at 11:44
  • When I save the first time isNew returns true, when I edit isNew returns false. After editing if I add what I think is a new group isNew returns false also. – Udders Jul 17 '13 at 12:59