2012-11-28 1 views
5

다음 코드를 사용하여 컬렉션에 새 모델을 만들 수 있습니다. 기본 데이터 저장소는 원격 API입니다 :백본 model.create 콜백을 호출하지 않음

 var postCreationStatus = this.model.create(newPostModel, { 
      wait : true  // waits for server to respond with 200 before adding newly created model to collection 
     }, { 
      success : function(resp){ 
       console.log('success callback'); 
       console.log(resp); 
      }, 
      error : function(err) { 
       console.log('error callback'); 
       console.log(err); 
      } 
     }); 

새로운 모델이 생성됩니다, 나는 데이터베이스에서이를 확인 할 수 있지만, 성공이나 오류도 콜백이 호출됩니다.

생성이 완료되면 사용자를 리디렉션하려고합니다. 조기에 리다이렉트하면 AJAX 요청이 없어 지므로 성공 콜백을 사용하는 것이 중요하다.

서버는 JSON 응답 { id : 11 }과 HTTP 상태가 200 OK 인 것으로 응답합니다.

+1

이 제목은 정확하지 않습니다.이 질문은 collection.create가 아닙니다. –

답변

6

백본 코드를 살펴보면 create() 함수에 대한 호출이 잘못되었음을 알게되었습니다. 성공 및 오류 콜백은 두 번째 인수로 전달되는 객체 내에 있어야하며 세 번째 인수로 전달되지 않아야합니다. 변경된 작업 스 니펫은 다음과 같습니다.

var postCreationStatus = this.model.create(newPostModel, { 
    wait : true, // waits for server to respond with 200 before adding newly created model to collection 

    success : function(resp){ 
     console.log('success callback'); 
     console.log(resp); 
     that.redirectHomePage(); 
    }, 
    error : function(err) { 
     console.log('error callback'); 
     // this error message for dev only 
     alert('There was an error. See console for details'); 
     console.log(err); 
    } 
});