Backbone.js验证模型属性

时间:2013-04-17 07:26:32

标签: validation backbone.js ruby-on-rails-3.2

我创造了一个非常简单的rails& amp;骨干应用程序到目前为止,验证是在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)当我运行时有活跃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.   

如果我不评论它没有任何反应......不知道为什么会发生这种情况

1 个答案:

答案 0 :(得分:0)

在Backbone模型上实现验证之前,您需要了解您在模型中实现的验证方法的准确程度。骨干验证可确保模型不会处于错误状态(包含错误数据)。这意味着如果您尝试在模型上设置导致验证失败的任何数据,则不会发送数据。在这种情况下,触发“无效”事件。

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

@collection.on "invalid", @handleInvalidState

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

这至少会告诉您实际上验证是否阻止了请求的发生。

基于源代码看来,如果模型无法在集合的create方法中进行验证,那么它将返回false。这可能就是你没有看到任何事情的原因。

希望这有帮助!