2013-04-17 61 views
3

我创建了一个相当简单的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. 

,如果我不评论它没有任何反应......不知道为什么之前你在你需要你的骨干机型执行验证这种情况

+0

这很奇怪,因为在这两种情况下都是这样做的:https:// github。COM/documentcloud /骨干网/ BLOB /主/ Backbone.js的#L872。你能否更准确地了解这两种情况会发生什么情况:验证是否失败?是否完成了对服务器的呼叫?服务器是否回答任何问题?另外,你使用的是什么版本的Backbone? – Loamhoof

+0

@Loamhoof原谅我久违的情况。请参阅更新1. – Elmor

回答

0

以了解如何使用您在模型中实现的验证方法。骨干验证确保模型不能处于不良状态(包含不良数据)。这意味着如果您尝试在导致验证失败的模型上设置任何数据,则不会发送数据。在这种情况下,触发“无效”事件。

所以作为一个起点,我首先要为集合上的“无效”事件添加一个回调。

@collection.on "invalid", @handleInvalidState 

handleInvalidState: (model, error) -> 
    // do something with the error. 

这将至少告诉你,如果实际上验证是阻止发生请求。

它出现在源代码的基础上,如果模型未能在集合的create方法中验证,它只会返回false。这可能是你看不到任何事情的原因。

希望这会有所帮助!

+0

谢谢,我现在就试试! – Elmor

+0

请参阅更新 – Elmor