b80c9fcf by Ean Schuessler

Segmented out from main source

1 parent cb96b40a
1 define(['rivets'], function(rivets) {
2 rivets.binders['error-*'] = {
3 bind: function(el) {
4 var self = this;
5 var holder = this.validationHolder = {
6 marker: el.parentNode.insertBefore(document.createComment(" rivets: " + this.type + " "), el),
7 focus: function() {
8 $(holder.container).removeClass('focused');
9 },
10 blur: function() {
11 holder.lastObj.seen(holder.lastId, true);
12 $(holder.container).addClass('focused');
13 holder.lastObj.validate();
14 },
15 validated: function(isValid, model, errors) {
16 var errorList = errors[holder.lastId];
17 var $container = $(holder.container);
18 var $fieldErrors = $container.find('> .validationfielderrors').empty();
19 if (errorList && holder.lastObj.seen(holder.lastId)) {
20 $container.addClass('has-errors');
21 require(['text!templates/fielderrorpopup.html!strip'], function(templateHtml) {
22 $fieldErrors.empty().append($($.parseHTML(templateHtml)).find('.template-content').append($.parseHTML(errorList)).end());
23 });
24 } else {
25 $container.removeClass('has-errors');
26 }
27 }
28 };
29 holder.container = $.parseHTML('<span class="validationerrorcontainer"><div class="validationfielderrors"></div></span>');
30 $(holder.container).removeClass('focused');
31 $(holder.marker).after($(holder.container).append(el));
32 diveIntoObject(this.model, this.keypath, function(obj, id) {
33 holder.lastObj = obj;
34 holder.lastId = id;
35 obj.on('validated', holder.validated);
36 });
37 $(el).on('focus', holder.focus).on('blur', holder.blur);
38 rivetsBinderCall(this, this.args[0], 'bind', arguments);
39 },
40 unbind: function(el) {
41 var holder = this.validationHolder;
42 $(this.validationHolder.marker).after(el).remove();
43 $(el).off('focus', holder.focus).off('blur', holder.blur);
44 diveIntoObject(this.model, this.keypath, function(obj, id) {
45 obj.off('validated', holder.validated);
46 });
47 delete this.validationHolder;
48 rivetsBinderCall(this, this.args[0], 'unbind', arguments);
49 },
50 routine: function(el, value) {
51 rivetsBinderCall(this, this.args[0], 'routine', arguments);
52 }
53 };
54 });