第一次尝试
试图让Backbone.Validation plugin正常工作。我宣布我要验证的属性...骨干验证插件始终验证为真
class window.Models.SearchQuery extends Backbone.Model
defaults:
city: ''
keywords: ''
lat: ''
long: ''
location: ''
performed_at: ''
region: ''
validation:
keywords:
required: true
pattern: 'number'
location:
required: true
pattern: 'number'
(我已经设置位置为数字只是用于测试) 然后在Chrome浏览器开发工具...
s = new Meg.Models.SearchQuery({validate:true})
s.set({'location': ''})
s.isValid()
// true
它总是通过验证..
第二次尝试
在验证混合成模型,WI其他一切都一样(如上)。
class App.Routers.AppRouter extends Backbone.Router
initialize: ->
_.extend(Backbone.Model.prototype, Backbone.Validation.mixin)
s = new App.Models.SearchQuery({validate:true})
//returns object..
m.set({'location': 'ewf3ef3ref3rf'})
//returns object with changed attrs
m.isValid('location')
//TypeError: Cannot call method 'call' of undefined