2012-03-06 62 views
1

因此,我使用backbone.js一次爲多個數據獲取多個條目。這些數據將最終形成一個時間表。我現在需要做的是弄清楚如何不斷檢查輸入到一個項目視圖中的三個數據字段,並在另一個項目視圖中重複。在Backbone.js驗證

下面是代碼

<div class="grid_16 lb-bg"> <div class="clearfix form-box bot-p bot-m"> 
<div > 
<label for="ClassTimes[{{ count }}].ClassId">Class</label> <br/> 
<select id="ClassTimes[{{ count }}].ClassId" name="ClassTimes[{{ count }}].ClassId" class="ClassList" style="Width: 20%"> 
    @foreach (var c in Model.Classes) 
    { 
     <option value="@c.Value">@c.Text</option> 
    } 
</select> 
</div> 
<div > 
<label for="ClassTimes[{{ count }}].RoomId">Room</label> <br/> 
<select id="ClassTimes[{{ count }}].RoomId" name="ClassTimes[{{ count }}].RoomId" style="Width: 20%"> 
    @foreach (var c in Model.Rooms) 
    { 
     <option value="@c.Value">@c.Text</option> 
    } 
</select> 
</div> 
<div > 
<label for="ClassTimes[{{ count }}].SessionId">Class Session</label> <br/> 
<select id="ClassTimes[{{ count }}].SessionId" name="ClassTimes[{{ count }}].SessionId" class="SessionList MakeWide" style="Width: 20%"> 
    @foreach (var c in Model.Sessions) 
    { 
     <option value="@c.Value">@c.Text</option> 
    } 
</select> 
</div> 
<div > 
<label for="ClassTimes[{{ count }}].DayId">Day</label> <br/> 
<select id="ClassTimes[{{ count }}].DayId" name="ClassTimes[{{ count }}].DayId" class="MakeWide" style="Width: 20%"> 
    @foreach (var c in Model.Days) 
    { 
     <option value="@c.Value">@c.Text</option> 
    } 
</select> 
</div> 


這裏是骨幹代碼

window.CreateAssign = (function() { 
    var CreateAssign = {}; 

    var subs = new Array(); 
    //The next of kin item list view 
    AssignItemView = Backbone.View.extend({ 
     tagName: 'div', 
     initialize: function() { 
      //bindall 
      _.bindAll(this, 'render'); 

      this.template = _.template($('#SFTemplate').html()); 

      this.render(); 
     }, 
     render: function() { 
      $(this.el).html(this.template({ 
       count: subs.length 
      })).fadeIn(); 
      return this; 
     } 

     , 
     remove: function() { 
      $(this.el).fadeOut('fast', function() { 
       $(this).remove(); 
      }); 
      return false; 
     } 
    }); 

    function subUpdated() { 
     if (subs.length > 0) { 
      $('#removeassign').fadeIn(); 
     } 
     else { 
      $('#removeassign').fadeOut(); 
     } 
    } 

    CreateAssign.Init = function() { 
     $('#addassign').click(function() { 
      var item = new AssignItemView(); 
      subs.push(item); 
      $('#classlist').prepend($(item.el).fadeIn('fast')); 
      subUpdated(); 
      return false; 
     }); 

     $('#removeassign').click(function() { 
      if (subs.length > 0) { 
       subs.pop().remove(); 
      } 
      subUpdated(); 
      return false; 
     }); 
    }; 
    return CreateAssign; 
})(this, this.document); 

$(function() { 
    CreateAssign.Init(); 
}); 

所以這段代碼是採取一些類和會話,r OOM和星期幾。

我的驗證需要確保同一會話中同一天沒有兩個類被選中。

我該如何去設置事件或任何我會用來查看在上一個itemview中選擇的數據?

+0

是'ClassTimes'骨幹集合嗎?它似乎是一個數組。是'ClassTimes'骨幹模型中的元素嗎?我只問,因爲你沒有使用'model.escape(「RoomId」)' – timDunham 2012-03-07 00:15:48

回答

0

我決定不嘗試在客戶端驗證,但在後面的代碼。在控制器中,我檢查了傳入的列表是否有任何此類衝突,並在發現任何此類時向表單返回了錯誤通知。工作得不錯。我正在努力突出顯示衝突項目。

1

好吧,很難說沒有看到你的任何骨幹公司,但如果你正在尋找模型驗證讓我刷我自己的軟件:Backbone.Validator

它是Backbone.Model類的混合,允許您向模型中添加通用驗證測試,並在驗證失敗時實現默認值(這也會觸發error消息)。

您可以對兩個視圖使用相同的模型並與驗證結合使用,以確保這些值在兩個視圖中均可用並經過驗證。