2016-06-28 2 views
-2

안녕하세요, 저는 모델을 데이터베이스에 저장하려고합니다. 내 ID가 자동 증가이기 때문에 제목을 저장하기 만하면됩니다. 나는 그것을 시도했지만 효과가 없었습니다. 누구든지 나를 도울 수 있습니까?모델을 데이터베이스에 저장하는 방법은 무엇입니까?

그리고 내 백본 모델에서 지정해야하는 urlRoot 또는 url 기능은 무엇입니까? 내 컬렉션의 URL을 지정해야합니까?

모델 : 여기

var DocumentUser = Backbone.Model.extend({ 
    urlRoot: '/app_dev.php/user' 
}); 

내 저장 기능 :

save: function(method, model, options) { 
    this.model = new DocumentUser(); 
    this.model.save({ 
     title: $('#title').val() 
    }, { 
     success: function(model, respose, options) { 
      console.log('The model has been saved to the server'); 
     }, 
     error: function(model, xhr, options) { 
      console.log('Something went wrong while saving the model'); 
     } 
    }); 
} 

답변

0

당신은 Backbone.sync에 위임하여, 데이터베이스 (또는 다른 영속 계층)에 모델을 저장하기 위해 백본 model.save([attributes], [options])을 사용할 수 있습니다 .

하면 모델 isNew의는 " create"(HTTP POST) 모델이 서버에 이미 존재하는 경우, (가) (HTTP PUT)는 " update"입니다 저장을 할 것이다 저장합니다.

코드 : 전 모델에 둘 필요가 어떤 URL

var DocumentUser = Backbone.Model.extend({ 
     default: { 
      title: '' 
     }, 
     urlRoot: '/app_dev.php/user' 
    }), 
    documentUser = new DocumentUser(); 

documentUser.save({ 
    title: $('#title').val() 
}, { 
    success: function(response) { 
     console.log('The model has been saved to the server', response); 
    }, 
    error: function(response) { 
     console.log('Something went wrong while saving the model', response); 
    } 
}); 
+0

그 주셔서 감사합니다,하지만? 모든 모델의 json 응답 또는 내 컬렉션의 URL? – neko

관련 문제