Remove use of diveIntoObject, and switch to using this.observer.
Showing
1 changed file
with
13 additions
and
55 deletions
1 | /* global Backbone */ | ||
2 | define(['rivets', 'bootstrap'], function(rivets) { | 1 | define(['rivets', 'bootstrap'], function(rivets) { |
3 | 'use strict'; | 2 | 'use strict'; |
4 | var rivetsBinderCall = function(binding, binderName, methodName, args) { | 3 | var rivetsBinderCall = function(binding, binderName, methodName, args) { |
... | @@ -14,42 +13,6 @@ define(['rivets', 'bootstrap'], function(rivets) { | ... | @@ -14,42 +13,6 @@ define(['rivets', 'bootstrap'], function(rivets) { |
14 | } | 13 | } |
15 | }; | 14 | }; |
16 | 15 | ||
17 | var diveIntoObject = function(obj, keypath, callback) { | ||
18 | if (!keypath) { | ||
19 | return callback(obj, null); | ||
20 | } | ||
21 | //return callback(obj, keypath); | ||
22 | var keyparts = keypath.replace(/^:/, '').split(/\:/); | ||
23 | //console.log('diveIntoObject(keyparts):', obj, keyparts); | ||
24 | while (keyparts.length > 1) { | ||
25 | var part = keyparts.shift(); | ||
26 | if (part.length === 0) { | ||
27 | continue; | ||
28 | } | ||
29 | //console.log('diveIntoObject:', obj, part); | ||
30 | obj = doObjectRead(obj, part); | ||
31 | } | ||
32 | //console.log('callback:', obj, keyparts[0]); | ||
33 | return callback(obj, keyparts.shift()); | ||
34 | }; | ||
35 | |||
36 | var doObjectRead = function(obj, id) { | ||
37 | if (obj === null) { | ||
38 | return obj; | ||
39 | } | ||
40 | if (!id) { | ||
41 | return obj; | ||
42 | } | ||
43 | //console.log('doObjectRead:', obj, id, obj instanceof Backbone.Model, obj instanceof Backbone.Collection); | ||
44 | if (obj instanceof Backbone.Model) { | ||
45 | return obj.get(id); | ||
46 | } else if (obj instanceof Backbone.Collection) { | ||
47 | return obj.at(id); | ||
48 | } else if (obj !== null) { | ||
49 | return obj[id]; | ||
50 | } | ||
51 | }; | ||
52 | |||
53 | rivets.binders['error-*'] = { | 16 | rivets.binders['error-*'] = { |
54 | bind: function(el) { | 17 | bind: function(el) { |
55 | var holder = this.validationHolder = { | 18 | var holder = this.validationHolder = { |
... | @@ -58,17 +21,17 @@ define(['rivets', 'bootstrap'], function(rivets) { | ... | @@ -58,17 +21,17 @@ define(['rivets', 'bootstrap'], function(rivets) { |
58 | $(holder.container).removeClass('focused'); | 21 | $(holder.container).removeClass('focused'); |
59 | }, | 22 | }, |
60 | blur: function() { | 23 | blur: function() { |
61 | if (holder.lastObj) { | 24 | if (holder.observer && holder.observer.target) { |
62 | holder.lastObj.seen(holder.lastId, true); | 25 | holder.observer.target.seen(holder.observer.key.path, true); |
63 | } | 26 | } |
64 | $(holder.container).addClass('focused'); | 27 | $(holder.container).addClass('focused'); |
65 | if (holder.lastObj) { | 28 | if (holder.observer && holder.observer.target) { |
66 | holder.lastObj.validate(); | 29 | holder.observer.target.validate(); |
67 | } | 30 | } |
68 | }, | 31 | }, |
69 | validated: function(isValid, model, errors) { | 32 | validated: function(isValid, model, errors) { |
70 | var errorList = errors[holder.lastId]; | 33 | var errorList = errors[holder.observer.key.path]; |
71 | if (errorList && holder.lastObj.seen(holder.lastId)) { | 34 | if (errorList && holder.observer.target && holder.observer.target.seen(holder.observer.key.path)) { |
72 | $(el).tooltip({title: errorList, trigger: 'focus'}); | 35 | $(el).tooltip({title: errorList, trigger: 'focus'}); |
73 | $(el).tooltip('show'); | 36 | $(el).tooltip('show'); |
74 | $(el).parent().addClass('has-error'); | 37 | $(el).parent().addClass('has-error'); |
... | @@ -85,26 +48,21 @@ define(['rivets', 'bootstrap'], function(rivets) { | ... | @@ -85,26 +48,21 @@ define(['rivets', 'bootstrap'], function(rivets) { |
85 | var holder = this.validationHolder; | 48 | var holder = this.validationHolder; |
86 | $(this.validationHolder.marker).after(el).remove(); | 49 | $(this.validationHolder.marker).after(el).remove(); |
87 | $(el).off('focus', holder.focus).off('blur', holder.blur); | 50 | $(el).off('focus', holder.focus).off('blur', holder.blur); |
88 | diveIntoObject(this.observer.target, this.observer.key.path, function(obj) { | 51 | if (holder.observer.target) { |
89 | if (obj) { | 52 | holder.observer.target.off('validated', holder.validated); |
90 | obj.off('validated', holder.validated); | ||
91 | } | 53 | } |
92 | }); | ||
93 | delete this.validationHolder; | 54 | delete this.validationHolder; |
94 | rivetsBinderCall(this, this.args[0], 'unbind', arguments); | 55 | rivetsBinderCall(this, this.args[0], 'unbind', arguments); |
95 | }, | 56 | }, |
96 | routine: function() { | 57 | routine: function() { |
97 | var holder = this.validationHolder; | 58 | var holder = this.validationHolder; |
98 | if (holder.lastObj) { | 59 | if (holder.observer) { |
99 | holder.lastObj.off('validated', holder.validated); | 60 | holder.observer.target.off('validated', holder.validated); |
100 | } | 61 | } |
101 | diveIntoObject(this.observer.target, this.observer.key.path, function(obj, id) { | 62 | holder.observer = this.observer; |
102 | holder.lastObj = obj; | 63 | if (this.observer.target) { |
103 | holder.lastId = id; | 64 | this.observer.target.on('validated', holder.validated); |
104 | if (obj) { | ||
105 | obj.on('validated', holder.validated); | ||
106 | } | 65 | } |
107 | }); | ||
108 | rivetsBinderCall(this, this.args[0], 'routine', arguments); | 66 | rivetsBinderCall(this, this.args[0], 'routine', arguments); |
109 | } | 67 | } |
110 | }; | 68 | }; | ... | ... |
-
Please register or sign in to post a comment