不执行骨干验证

时间:2013-02-02 15:05:43

标签: javascript validation backbone.js coffeescript

似乎没有考虑我的Coffeescript验证:

 class PokerRange extends Backbone.Model
    defaults:
        id:0
        cards:[[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12]]

    validate: (attributes) ->
        if attributes.id<0 then "id should be positive"

window.firstrange= new PokerRange id:5
console.log window.firstrange.toJSON()
window.firstrange.set("id",-4)
console.log window.firstrange.toJSON()

结果如下:

Object {id: 5, cards: Array[13]}
Object {id: -4, cards: Array[13]}

1 个答案:

答案 0 :(得分:2)

我发现了原因,我只是忘了将set方法作为选项传递{validate:true}

    class PokerRange extends Backbone.Model
    defaults:
        id:0
        cards:[[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12],[0..12]]
    validate: (attributes) ->
        if attributes.id<0 then "id should be positive"

window.firstrange= new PokerRange id:5

window.firstrange.on "invalid", (model, error) ->  alert(error)


console.log window.firstrange.toJSON()
window.firstrange.set {id:-4}, {validate:true}
console.log window.firstrange.toJSON()
相关问题