非常にシンプルな Rails & backbone アプリを作成しました。今まではRails側でバリデーションを行っていました。今、バックボーンの検証を実装することを考えました。私はこれをやっています:
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>
あなたのコメントの時点で: 私は gems/backbone-on-rails-1.0.0.0 を使用しています
情報が入力されていません1)実行時
にアクティブで
クロムを使用しています。そして、送信ボタン(トリガーcreateCampaign)をクリックしてもフィールドを空のままにしても何も起こりません!コンソールとネットワークのタブを見ています
2)コメントアウト: 成功のためのコールバックが実行され、その後何も起こりません入力された情報は、新しいモデルの
有無にかかわらず、DB に作成されますwait: true
wait: true
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
コメントアウトするwait: true
と、このコンソール出力が得られます
validation
Campaign {cid: "c2", attributes: Object, collection: Campaigns, _changing: false, _previousAttributes: Object…}
Please fill start time of the campaign.
コメントアウトしないと何も起こりません...なぜこれが起こるのかわかりません