我是Backbone的新手。我需要使用服务器对save()方法的响应中的数据,但我不知道如何获取它。这是代码:
$(".act_btn").on("click", function(){
var act_id = $(this).attr("data-id");
startRecordItem = new StartRecordItem({
activity: act_id,
});
startRecordItem.save({
success: function(response){console.log(response)}
});
/*
recordItem = new RecordItem({
id: ... <---- I have to populate this with the data from the server response.
});
*/
现在成功功能根本不起作用,我错过了什么?我想从JSON响应中获取一个属性,然后在'new RecordItem'上使用它。保存本身正常工作,响应如下所示:
{"activity": 1, "id": 14}
答案 0 :(得分:1)
您遗失的是.save
的第一个参数。是传递到模型中的属性,预先保存。你想做更像这样的事情:
startRecordItem.save(null, {
success: function(response) {
console.log(response);
}
});
由于Backbone在成功保存时触发sync
事件,您也可以这样做:
this.listenTo(startRecordItem, 'sync', function() { /* logic here */ });