BB: Validation Plugin
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.validation/0.11.5/backbone-validation.js"></script>
<h1>Backbone Validation Ref</h1>
<script type="text/template" class="tmpl-person">
<form action="" class="form-fields">
<div>
<label>Favorite Name:</label>
<input type="text" class="js-input" name="favName" />
<span class="feedback"></span>
<ul class="error">This is a required field</ul>
</div>
</form>
<br>
<button class="btn js-action--validate-favourite">Validate favourite name</button>
</script>
SCSS
@import url(http://fonts.googleapis.com/css?family=Roboto:300,400,500);
* {
-webkit-font-smoothing: antialiased;
box-sizing: border-box;
}
body {
font-family: Roboto;
padding: 16px;
line-height: 1.5;
font-weight: normal;
color: #ccc;
background: #292929;
font-size: 18px;
}
h1, h2, h3 {
font-weight: normal;
}
.pg-heading {
margin-bottom: 0;
}
h1 {
margin-bottom: 20px;
font-size: 28px;
}
a {
color: #95baf6;
transition: opacity .3s;
}
a:hover {
opacity: 0.75;
}
div {
margin-bottom: 8px;
}
input,
button{
border: none;
height: 40px;
outline: none;
padding: 0 8px;
}
button {
margin-right: 8px;
margin-bottom: 8px;
height: 50px;
}
input {
width: 100%;
}
label {
display: block;
}
button {
padding: 0 16px;
background: #2196F3;
color: white;
transition: background .3s;
font-size: 18px;
border-radius: 4px;
}
button:hover {
background: #1E88E5;
}
button:active {
background: #1976D2;
}
input[type=text] {
transition: border-color .3s;
border: 1px solid white;
}
input ~ .error {
color: red;
display: none;
}
input.invalid ~ .error {
display: block;
}
Babel + JSX
console.clear();
const Model = Backbone.Model.extend({
validation: {
favName: [{
required: true,
minLength: 3,
msg: 'Name must be at least 3 characters long'
}, {
required: true,
maxLength: 20,
msg: 'Name can be no longer than 20 characters long'
}, {
required: true,
msg: 'Name already exists',
fn: 'checkForDuplicateName'
}]
},
checkForDuplicateName(name) {
if (favsCollection.findWhere({
'favName': name
})){
return 'error'
}
}
});
const Collection = Backbone.Collection.extend({
initialize: function(models, options){
this.options = options;
this.listenTo(this, 'add', this.addToCollection, this);
},
addToCollection: function(model){
this.add(model);
}
});
const View = Backbone.View.extend({
template: Handlebars.compile( $('.tmpl-person').html() ),
className: 'view',
events: {
'click .js-action--validate-favourite': 'actionValidate'
},
initialize() {
Backbone.Validation.bind(this);
this.fragment = document.createDocumentFragment();
this.listenTo(this.model, {
'validated': this.onModelValidated,
'validated:valid': this.onModelValid,
'validated:invalid': this.onModelInvalid,
'invalid': this.onInvalid
});
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
console.log('MSIE: ', msie);
},
render() {
this.$el.html(this.template(this.model.toJSON()));
return this;
},
/**
* Save favourite clicked, set keys to 'favName' to match the input to validate
* calls main validation function with keys and callback function
* @param {object} Click event from button
*/
actionValidate(event){
event.preventDefault();
var keys = 'favName';
this.actionValidate(keys, this.saveFavourite.bind(this));
},
/**
* validate keys, get all inputs, create 'modelValues' obj, set model to this obj
* sets 'isValid' top validate the keys
* @param ...