2014-09-04 32 views
-1

我需要使用型号ID从COLLECTION.create呼叫重定向用户做新的一页如何从collection.create调用中获取model.id?

window.href = 'https://example.com/document/' + model.id 

我不在乎,如果AJAX调用REST API是异步或同步(如果该事项)。我希望我能做这样的事情:

var OPTS ={} 
OPTS['success'] = function(response, model, options){ 
       model.id 
} 

SOMECOLLECTION.create(json_attributes,OPTS) 

但这并不行。我正在使用Django-Tastypie作为我的REST API。

+0

为什么这是downvoted? – josephmisiti 2014-09-06 23:23:53

回答

1

这应该工作

var MyCollection = Backbone.Collection.extend({ 
    url: '/echo/json/' 
}); 

var my_collection = new MyCollection(); 

var model = my_collection.create({ name: "Eugene", age: 31 }, { 

    success: function(response, model) { 
     console.log(model.id); 
     // id: 123 
    } 

}); 

console.log(model.id); 
// id: undefined 

我创建工作示例这里http://jsfiddle.net/6wup7q9e/3/

请检查您的网络选项卡,看到你从你的REST API有什么反应,它应该有和你json_response和新id属性这将被用作模型ID。在我的情况下,它会是这样的:

{ 
    id: 123, 
    name: "Eugene", 
    age: 31 
} 
相关问题