2014-09-04 5 views
-1

는 그 중요한 경우 나머지 API에 아약스 호출 (비동기 또는 동기의 경우 난 상관 없어 새 페이지를collection.create 호출에서 model.id를 어떻게 구합니까?

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

을 수행 할 사용자를 리디렉션 할 COLLECTION.create 호출에서 모델 ID를 사용할 필요가). 나는 다음과 같이 할 수 있기를 바랬다 :

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

SOMECOLLECTION.create(json_attributes,OPTS) 

그러나 그것은 작동하지 않는다. 내 REST API로 Django-Tastypie를 사용하고 있습니다.

+0

이것이 왜 downvoted입니까? – josephmisiti

답변

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/

가 있고 당신의 json_response 새로운 id 속성을해야한다, 당신이 당신의 REST API에서 무슨 응답을 확인하려면 네트워크 탭을 확인하시기 바랍니다 모델 ID로 사용됩니다. 내 경우에는 다음과 같을 것입니다 :

{ 
    id: 123, 
    name: "Eugene", 
    age: 31 
} 
관련 문제