backbone.validation 插件。验证不适用于模型
backbone.validation plugin. Validation doesn't work on model
我尝试使用 Backbone.validation 插件。
这是此插件的 link。
https://github.com/thedersen/backbone.validation.
我想在创建新模型实例时开始验证。通常在 Backbone 应该实现一个 validate 函数然后当我们应该通过 {validate:true}
如何使用此插件实现相同的结果?
//create picture instance in a controller
var model = new Picture({
name: file.name,
size: file.size,
type: file.type
}, {validate: true} );
//Picture class
export default Backbone.Model.extend({
defaults: {
name: "",
size: null,
type: ""
},
validation: {
size: function (size) {
if(size > this.MAX_FILE_SIZE;) {
return this.onFileSizeError()
}
return '';
},
onFileSizeError() {
//execute this when model size is wrong
}
});
问题已解决
所以在插件文档中是关于在不绑定视图的情况下验证模型的信息。
Using model validation
The philosophy behind this way of using the plugin, is to give you an
easy way to implement validation across all your models without the
need to bind to a view. Of course, if you use this option the
callbacks to update the view is not executed, since there is no way of
knowing what view a model belongs to.
Validation mix-in
To add validation to your models, mix in the validation on the Model's
prototype.
_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
因此我们可以创建一个包含基础模型的文件,该模型将被扩展 Backbone.Model.prototype。现在每个新的 class 都应该由我们的 class 模型扩展。
从现在开始我们可以在实例上使用 validation.plugin
我尝试使用 Backbone.validation 插件。
这是此插件的 link。
https://github.com/thedersen/backbone.validation.
我想在创建新模型实例时开始验证。通常在 Backbone 应该实现一个 validate 函数然后当我们应该通过 {validate:true}
如何使用此插件实现相同的结果?
//create picture instance in a controller var model = new Picture({ name: file.name, size: file.size, type: file.type }, {validate: true} );
//Picture class
export default Backbone.Model.extend({
defaults: {
name: "",
size: null,
type: ""
},
validation: {
size: function (size) {
if(size > this.MAX_FILE_SIZE;) {
return this.onFileSizeError()
}
return '';
},
onFileSizeError() {
//execute this when model size is wrong
}
});
问题已解决
所以在插件文档中是关于在不绑定视图的情况下验证模型的信息。
Using model validation
The philosophy behind this way of using the plugin, is to give you an easy way to implement validation across all your models without the need to bind to a view. Of course, if you use this option the callbacks to update the view is not executed, since there is no way of knowing what view a model belongs to.
Validation mix-in
To add validation to your models, mix in the validation on the Model's prototype.
_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
因此我们可以创建一个包含基础模型的文件,该模型将被扩展 Backbone.Model.prototype。现在每个新的 class 都应该由我们的 class 模型扩展。
从现在开始我们可以在实例上使用 validation.plugin