0

I'm trying to update data on backbone.But my save() function post insead of put.

editEvent:function(e){
         var departments=new Department();
       departments.set("departmentName",this.$el.find(".departmentName").val());
            departments.save();

            this.render();

        },

departmentModel

    var Department = Backbone.Model.extend({
    urlRoot: "/rest/department",
    idAttribute:'departmentID'

    }
});
return Department;

template

    <th>
    <span>{{user.department.departmentName}}</span>
    <input data-placement="top" title="Boş geçilemez!" class="form-control text-center departmentName" type='text'
           style='display: none;'  value='{{user.department.departmentName}}'/>
</th>
real
  • 53
  • 1
  • 2
  • 9
  • I wrote an [extensive answer on how to force a POST request with Backbone](http://stackoverflow.com/a/41091957/1218980). Most technique could be used to force a PUT request. – Emile Bergeron Apr 24 '17 at 14:03

2 Answers2

2

That's by design, here's a snippet from the Backbone docs for the save function:

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).

Model is considered 'New' if it does not have an id.

Since you're creating the model for the first time, Backbone will use the POST method as it should, because it means you're creating a new resource. For more info on the meaning of HTTP verbs.

My suggestion would be solving this on the server side and making separate handlers for POST and PUT, but if you really want to force PUT method, here are some tips: What is the least ugly way to force Backbone.sync updates to use POST instead of PUT?

Community
  • 1
  • 1
Mario
  • 128
  • 7
0

I solved the problem with using id.If you try PUT request without id,backbone think "it's a new model".But you save with data's id value, backbone think PUT request.

That's why i tried put request with id and that's worked.

departments.set("departmentID",this.$el.find(".departmentName").data("id"));
          departments.set("departmentName",this.$el.find(".departmentName").val());
          departments.save();
real
  • 53
  • 1
  • 2
  • 9