我创建了一个相当简单的rails &主干应用程序。到目前为止,验证是在导轨侧执行的。现在我想到了实施骨干验证。我这样做: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)with ac tive wait: true
当我运行
我使用铬。如果我点击提交按钮(触发器createCampaign)离开字段为空没有发生!我期待在控制台&网络选项卡
2)wait: true
注释掉:回调成功运行,那么什么都不会发生
信息进入
带或不带wait: true
新模式DB创建
更新
加入的视图初始化:
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.
,如果我不评论它没有任何反应......不知道为什么之前你在你需要你的骨干机型执行验证这种情况
这很奇怪,因为在这两种情况下都是这样做的:https:// github。COM/documentcloud /骨干网/ BLOB /主/ Backbone.js的#L872。你能否更准确地了解这两种情况会发生什么情况:验证是否失败?是否完成了对服务器的呼叫?服务器是否回答任何问题?另外,你使用的是什么版本的Backbone? – Loamhoof
@Loamhoof原谅我久违的情况。请参阅更新1. – Elmor