rivets-error-binder.js
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
define(['rivets', 'bootstrap'], function(rivets) {
var rivetsBinderCall = function(binding, binderName, methodName, args) {
var binder = rivets.binders[binderName];
if (binder instanceof Function) {
if (methodName == 'routine') {
binder.apply(binding, args);
};
} else if (binder) {
if (binder[methodName]) {
binder[methodName].apply(binding, args);
}
}
}
var diveIntoObject = function(obj, keypath, callback) {
if (!keypath) {
return callback(obj, null);
}
//return callback(obj, keypath);
var keyparts = keypath.replace(/^:/, '').split(/\:/);
//console.log('diveIntoObject(keyparts):', obj, keyparts);
while (keyparts.length > 1) {
var part = keyparts.shift();
if (part.length == 0) {
continue;
}
//console.log('diveIntoObject:', obj, part);
obj = doObjectRead(obj, part);
}
//console.log('callback:', obj, keyparts[0]);
return callback(obj, keyparts.shift());
};
var doObjectRead = function(obj, id) {
if (obj === null) return obj;
if (!id) return obj;
//console.log('doObjectRead:', obj, id, obj instanceof Backbone.Model, obj instanceof Backbone.Collection);
if (obj instanceof Backbone.Model) {
return obj.get(id);
} else if (obj instanceof Backbone.Collection) {
return obj.at(id);
} else if (obj != null) {
return obj[id];
}
};
rivets.binders['error-*'] = {
bind: function(el) {
var self = this;
var holder = this.validationHolder = {
//marker: el.parentNode.insertBefore(document.createComment(" rivets: " + this.type + " "), el),
focus: function() {
$(holder.container).removeClass('focused');
},
blur: function() {
holder.lastObj.seen(holder.lastId, true);
$(holder.container).addClass('focused');
holder.lastObj.validate();
},
validated: function(isValid, model, errors) {
var errorList = errors[holder.lastId];
if (errorList && holder.lastObj.seen(holder.lastId)) {
$(el).tooltip({title: errorList, trigger: 'focus'});
$(el).tooltip('show');
$(el).parent().addClass('has-error');
} else {
$(el).tooltip('destroy');
$(el).parent().removeClass('has-error');
}
}
};
diveIntoObject(this.observer.target, this.observer.key.path, function(obj, id) {
holder.lastObj = obj;
holder.lastId = id;
obj.on('validated', holder.validated);
});
$(el).on('focus', holder.focus).on('blur', holder.blur);
rivetsBinderCall(this, this.args[0], 'bind', arguments);
},
unbind: function(el) {
var holder = this.validationHolder;
$(this.validationHolder.marker).after(el).remove();
$(el).off('focus', holder.focus).off('blur', holder.blur);
diveIntoObject(this.model, this.keypath, function(obj, id) {
obj.off('validated', holder.validated);
});
delete this.validationHolder;
rivetsBinderCall(this, this.args[0], 'unbind', arguments);
},
routine: function(el, value) {
rivetsBinderCall(this, this.args[0], 'routine', arguments);
}
};
});