Rewrite the holder to be a real object, that uses Backbone.Events to
dispatch everything.
Showing
1 changed file
with
42 additions
and
33 deletions
... | @@ -2,6 +2,7 @@ define(function(require) { | ... | @@ -2,6 +2,7 @@ define(function(require) { |
2 | 'use strict'; | 2 | 'use strict'; |
3 | var module = require('module'); | 3 | var module = require('module'); |
4 | var rivets = require('rivets'); | 4 | var rivets = require('rivets'); |
5 | var Backbone = require('backbone'); | ||
5 | var $ = require('jquery'); | 6 | var $ = require('jquery'); |
6 | 7 | ||
7 | var rivetsBinderCall = function(binding, binderName, methodName, args) { | 8 | var rivetsBinderCall = function(binding, binderName, methodName, args) { |
... | @@ -32,57 +33,65 @@ define(function(require) { | ... | @@ -32,57 +33,65 @@ define(function(require) { |
32 | } | 33 | } |
33 | }; | 34 | }; |
34 | 35 | ||
35 | rivets.binders['error-*'] = { | 36 | function createTriggerProxy(obj, eventName) { |
36 | bind: function(el) { | 37 | return function() { |
37 | var observer = this.observer; | 38 | obj.trigger.apply(obj, [eventName].concat(_.toArray(arguments))); |
39 | }; | ||
40 | } | ||
41 | |||
42 | function ErrorHandler(binding, el) { | ||
43 | var seen = {}; | ||
44 | var observer = binding.observer; | ||
45 | var oldParent; | ||
46 | var parentObserver; | ||
38 | var keyPath = observer.key.path; | 47 | var keyPath = observer.key.path; |
39 | var holder = this.validationHolder = { | 48 | this.on('bind', function() { |
40 | //marker: el.parentNode.insertBefore(document.createComment(" rivets: " + this.type + " "), el), | 49 | render(el, 'bind'); |
41 | observer: observer, | 50 | render(el, 'validated', false); |
42 | seen: {}, | 51 | }); |
43 | focus: function() { | 52 | this.on('focus', function() { |
44 | render(el, 'focus', false); | 53 | render(el, 'focus', false); |
45 | }, | 54 | }); |
46 | blur: function() { | 55 | this.on('blur', function() { |
47 | holder.seen[keyPath] = true; | 56 | seen[keyPath] = true; |
48 | render(el, 'blur', false); | 57 | render(el, 'blur', false); |
49 | if (observer.target) { | 58 | if (observer.target) { |
50 | observer.target.validate(); | 59 | observer.target.validate(); |
51 | } | 60 | } |
52 | }, | 61 | }); |
53 | validated: function(isValid, model, errors) { | 62 | this.on('validated', function(isValid, model, errors) { |
54 | var errorList = errors[keyPath]; | 63 | var errorList = errors[keyPath]; |
55 | if (errorList && holder.seen[keyPath]) { | 64 | if (errorList && seen[keyPath]) { |
56 | render(el, 'validated', errorList); | 65 | render(el, 'validated', errorList); |
57 | } else { | 66 | } else { |
58 | render(el, 'validated', false); | 67 | render(el, 'validated', false); |
59 | } | 68 | } |
69 | }); | ||
70 | this.on('unbind', function() { | ||
71 | render(el, 'unbind'); | ||
72 | $(el).off('.rivets-error-binder'); | ||
73 | }); | ||
74 | var $el = $(el); | ||
75 | $el.on('focus.rivets-error-binder', createTriggerProxy(this, 'focus')); | ||
76 | $el.on('blur.rivets-error-binder', createTriggerProxy(this, 'blur')); | ||
77 | if (observer.target) { | ||
78 | this.listenTo(observer.target, 'validated', createTriggerProxy(this, 'validated')); | ||
79 | } | ||
60 | } | 80 | } |
61 | }; | 81 | _.extend(ErrorHandler.prototype, Backbone.Events); |
62 | $(el).on('focus.rivets-error-binder', holder.focus).on('blur.rivets-error-binder', holder.blur); | 82 | |
83 | rivets.binders['error-*'] = { | ||
84 | bind: function(el) { | ||
85 | this.errorHandler = new ErrorHandler(this, el); | ||
86 | this.errorHandler.trigger('bind'); | ||
63 | rivetsBinderCall(this, this.args[0], 'bind', arguments); | 87 | rivetsBinderCall(this, this.args[0], 'bind', arguments); |
64 | render(el, 'bind'); | ||
65 | render(el, 'validated', false); | ||
66 | }, | 88 | }, |
67 | unbind: function(el) { | 89 | unbind: function(el) { |
68 | var holder = this.validationHolder; | 90 | this.errorHandler.trigger('unbind'); |
69 | render(el, 'unbind'); | 91 | delete this.errorHandler; |
70 | $(this.validationHolder.marker).after(el).remove(); | ||
71 | $(el).off('.rivets-error-binder'); | ||
72 | if (holder.observer.target) { | ||
73 | holder.observer.target.off('validated', holder.validated); | ||
74 | } | ||
75 | delete this.validationHolder; | ||
76 | rivetsBinderCall(this, this.args[0], 'unbind', arguments); | 92 | rivetsBinderCall(this, this.args[0], 'unbind', arguments); |
77 | }, | 93 | }, |
78 | routine: function() { | 94 | routine: function() { |
79 | var holder = this.validationHolder; | ||
80 | if (this.observer.target) { | ||
81 | this.observer.target.off('validated', holder.validated); | ||
82 | } | ||
83 | if (this.observer.target) { | ||
84 | this.observer.target.on('validated', holder.validated); | ||
85 | } | ||
86 | rivetsBinderCall(this, this.args[0], 'routine', arguments); | 95 | rivetsBinderCall(this, this.args[0], 'routine', arguments); |
87 | } | 96 | } |
88 | }; | 97 | }; | ... | ... |
-
Please register or sign in to post a comment