javascriptjavascriptmvccanjscanjs-model

How to make can.Model send JSON data when update?


I have this code:

var SotriesModel = can.Model.extend({
    findAll: 'GET /api/stories/',
    create: function(story) {
        console.log(story)
        return $.ajax({
            url: '/api/stories/',
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(story)
        });
    },
    update : function(story) {
        console.log(story)
        return $.ajax({
            url: '/api/stories/',
            type: 'PUT',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(story)
        });
    },
    destroy: 'DELETE /api/stories/{id}'
}, {});
var story = new SotriesModel({id:123123, name:"test"});
story.save();

What I expect from save is a PUT request with a JSON in payload, but what I get is just the id number:

123123

Solution

  • The update method signature for using a function is can.Model.update: function(id, serialized) -> can.Deffered.

    So as the first parameter you always get the id and as the second the serialized data.

    Changing your update : function(story) {} to update : function(id, story) {} should solve the problem.