Skip to content

Commit

Permalink
Adding support for
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorm Casper committed Feb 5, 2015
1 parent eae66e4 commit 3b59095
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
43 changes: 37 additions & 6 deletions nod.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function nod () {
var form,
configuration = {},
mediator = nod.makeMediator(),
eventEmitter = nod.makeEventEmitter(mediator),

// Creating (empty) collections
listeners = nod.makeCollection(nod.makeListener),
Expand Down Expand Up @@ -190,12 +191,15 @@ function nod () {
metricSet.checkHandler.subscribeTo(checkId, metric.errorMessage, metric.defaultStatus);


if (configuration.noDom) {
eventEmitter.subscribe(metricSet.checkHandler.id);
} else {
// :: checkHandler -> domNode

// :: checkHandler -> domNode

// The checkHandler has its own id (and only ever needs one), so we
// just ask the domNode to listen for that.
metricSet.domNode.subscribeTo(metricSet.checkHandler.id);
// The checkHandler has its own id (and only ever needs one), so we
// just ask the domNode to listen for that.
metricSet.domNode.subscribeTo(metricSet.checkHandler.id);
}
});


Expand Down Expand Up @@ -650,6 +654,7 @@ nod.makeCheckHandler = function (element, mediator, configuration) {
id: id,
type: 'result',
result: status.status,
element: element,
errorMessage: status.errorMessage
});
}
Expand Down Expand Up @@ -752,7 +757,10 @@ nod.makeDomNode = function (element, mediator, configuration) {
customSpan = false;

span.style.display = 'none';
parent.appendChild(span);

if (!configuration.noDom) {
parent.appendChild(span);
}

// Updates the class of the parent to match the status of the element.
function updateParent (status) {
Expand Down Expand Up @@ -866,6 +874,29 @@ nod.makeDomNode = function (element, mediator, configuration) {
};


nod.makeEventEmitter = function (mediator) {
var customEvent;

function emit (options) {
if (CustomEvent) {
customEvent = new CustomEvent('nod.validation', {detail: options});

options.element.dispatchEvent(customEvent);
} else {
throw('nod.validate tried to fire a custom event, but the browser does not support CustomEvent\'s');
}
}

function subscribe (id) {
mediator.subscribe(id, emit);
}

return {
subscribe: subscribe
};
};


/**
* getElement
*
Expand Down
36 changes: 36 additions & 0 deletions tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -932,5 +932,41 @@
</script>



<form action='' id='s_form'>
<input tpye='text' id='s'>
</form>

<script>
(function () {
var r_nod = nod();

r_nod.configure('noDom', true);

r_nod.add({
selector: '#s',
validate: 'presence',
errorMessage: ERROR_MSG
});

var s = get('s'),
s_form = get('s_form');

expect(s_form.children.length === 1, 'DOM Was not touch with `noDom`');

var s_check = false;

s.addEventListener('nod.validation', function () { s_check = true; }, false);

setValue(s, 'foo');

expect(s_check, 'Event is fired on the element with `noDom` set');

s.removeEventListener('nod.validation');

removeElement(s.parentNode);
})();
</script>

</body>
</html>

0 comments on commit 3b59095

Please sign in to comment.