2

私の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]}
4

1 に答える 1

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()
于 2013-02-02T15:28:38.537 に答える