매우 간단한 레일 & 백본 앱을 만들었습니다. 지금까지 레일 측에서는 검증이 수행되었습니다. 이제 백본 유효성 검사를 구현하는 방법에 대해 생각했습니다. 나는 이렇게하고있다 :Backbone.js 유효성 확인 모델 속성
createCampaign: (e) ->
_this= @
e.preventDefault()
attributes =
info: @getCountries()
time_start: @$('#start').val()
time_end: @$('#end').val()
@collection.create attributes,
wait: true
success: ->
@$('#errors').text('Campaign created!').addClass('correct')
@$('#create').removeAttr('disabled');
_this.clearFields('new_campaign')
error: -> @handleError
wait: true
와 함께 아무 일도 일어나지 않는다. 만약 내가 그것을 코멘트 성공하면 조치가 취해집니다. 비록 내가 의도에 필요한 데이터를 제공하지는 않는다.
내 모델 & 수집
class SvitlaTest.Models.Campaign extends Backbone.Model
initialize: ->
@bind("error", @errorHandling)
validate: (attrs) ->
return "Please fill start time of the campaign." unless attrs.time_start
return "Please fill end time of the campaign." unless attrs.time_end
"Please fill Countrz & language fields." unless attrs.info
errorHandling: (model, event) ->
@$('#create').removeAttr('disabled');
@$('#errors').html(error)
class SvitlaTest.Collections.Campaigns extends Backbone.Collection
url: '/api/campaigns'
model: SvitlaTest.Models.Campaign
업데이트 1
내 템플릿 jst.eco
<form method="post" id="new_campaign" class="corners">
<p>
<label for="start" >Start time:</label>
<input type="text" id="start" name="start" autofocus="" length='30' maxlength='30' />
</p>
<p>
<label for="end" >End time:</label>
<input type="text" id="end" name="end" length='30' maxlength='30' />
</p>
<div id='country_list'>
<h4>Info:</h4>
<p class="country_element">
Country
<input type="text" class='country' id="country" />
Languages
<input type="text" class="languages" id="languages" />
</p>
</div>
<p>
<input type="submit" id="create" value="Create" />
</p>
</form>
의견의로 : 나는 보석을 사용하고 /백본에 레일 - 1.0 .0.0
입력 한 정보 없음
1) ac tive wait: true
내가 달릴 때
나는 크롬을 사용하고 있습니다. 필드를 비워두면 제출 버튼 (트리거 createCampaign)을 클릭하면 아무것도 표시되지 않습니다.! 성공을위한 콜백이 실행 후 아무것도
정보를 DB에서 생성 또는 wait: true
새로운 모델없이
를 입력
업데이트를 발생하지 : 나는 콘솔 & 네트워크 탭
2) wait: true
이 주석을 찾고 있어요
initialize: ->
@collection.on('reset', @render,this)
@collection.on('add', @appendCampaign ,this)
@collection.on("invalid", @handleInvalidState)
handleInvalidState: (model, error) ->
console.log "validation"
console.log model
console.log error
I :
보기의 초기화 첨가 과학 코멘트 아웃 wait: true
는 내가이 콘솔 출력을 얻을
validation
Campaign {cid: "c2", attributes: Object, collection: Campaigns, _changing: false, _previousAttributes: Object…}
Please fill start time of the campaign.
내가 아무 일을 주석 처리하지 않는 경우에 당신은 당신이 필요로하는 당신의 백본 모델에 유효성 검사를 구현하기 전에이
같은 상황이 https : // github에서 모두 발생하므로 매우 이상합니다.co.kr/documentcloud/backbone/blob/master/backbone.js # L872를 참조하십시오. 두 경우 모두 어떤 일이 발생하는지에 대해 더 정확하게 설명해 줄 수 있습니까? 유효성 검사가 실패합니까? 서버 호출이 완료 되었습니까? 서버가 아무것도 응답합니까? 또한 백본의 어떤 버전을 사용합니까? – Loamhoof
@Loamhoof 내 장기 결석을 변명. 업데이트 1을 참조하십시오. – Elmor