Skip to content

Instantly share code, notes, and snippets.

@alustau
Last active August 29, 2015 14:17
Show Gist options
  • Save alustau/370a674fdfb46d4af389 to your computer and use it in GitHub Desktop.
Save alustau/370a674fdfb46d4af389 to your computer and use it in GitHub Desktop.
Aqui fica o arquivo de configuração
<!DOCTYPE html>
<html>
<head>
<title>
Jquery
</title>
<script data-main="js/app" src="js/libs/require.js"></script>
</head>
<body>
<form id="formulario">
<input type="text" value="2" data-form-validate="required">
<input type="submit" value="Enviar">
</form>
</body>
</html>
requirejs.config({
baseUrl: "js",
app: 'app',
paths: {
jquery: 'libs/jquery',
form: 'app/form/form',
validate: 'app/form/validator/validate',
required: 'app/form/validator/required',
// validate: 'app/form/validates/',
// appDir: 'app'
}
});
requirejs(["app/main"]);
define(function(require) {
var $form,
$ = require('jquery');
function Form(selector) {
$form = $(selector);
}
Form.prototype.getElements = function() {
var $elements = $form.find('select, input[type="text"], textarea, hidden');
return $elements;
};
Form.prototype.isValid = function() {
var valid = true;
this.getElements().each(function() {
var validate = $(this).getValidate();
if (null !== validate && !validate.isValid($(this).val())) {
valid = false;
}
});
return false;
return valid;
};
$.fn.getValidate = function() {
var name = this.data('form-validate');
if (typeof name !== 'undefined') {
//o erro ta aqui
var validate = require(name);
console.log(validate);return false;
var validate = eval('new '+ name);
if (!validate instanceof Validate) {
throw "Este objeto tem que ser parente de Validate";
}
return validate;
}
return null;
};
// function getValidate($this) {
// }
return (Form);
});
define(['validate', 'require'], function(Validate) {
Required.prototype = new Validate();
function Required() {}
Required.prototype.isValid = function(value) {
if (typeof value === 'undefined' || !value) {
return false;
}
return true;
}
return Required;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment