第一次尝试
试图让Backbone.Validation 插件工作。我已经声明了要验证的属性...
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
它总是通过验证..
第二次尝试
将验证混合到模型中,其他一切都相同(如上)。
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