如何使can.Model在更新时发送JSON数据?

时间:2013-09-04 10:24:49

标签: javascript javascriptmvc canjs canjs-model

我有这段代码:

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();

我对save的期望是带有JSON的PUT请求,但我得到的只是id号:

123123

1 个答案:

答案 0 :(得分:0)

使用函数的update method signaturecan.Model.update: function(id, serialized) -> can.Deffered

因此,作为第一个参数,您始终获得id,并将第二个参数作为序列化数据。

update : function(story) {}更改为update : function(id, story) {}可以解决问题。