8f826811 by Adam Heath

Remove use of diveIntoObject, and switch to using this.observer.

1 parent 8b43b4d6
/* global Backbone */
define(['rivets', 'bootstrap'], function(rivets) {
'use strict';
var rivetsBinderCall = function(binding, binderName, methodName, args) {
......@@ -14,42 +13,6 @@ define(['rivets', 'bootstrap'], function(rivets) {
}
};
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 holder = this.validationHolder = {
......@@ -58,17 +21,17 @@ define(['rivets', 'bootstrap'], function(rivets) {
$(holder.container).removeClass('focused');
},
blur: function() {
if (holder.lastObj) {
holder.lastObj.seen(holder.lastId, true);
if (holder.observer && holder.observer.target) {
holder.observer.target.seen(holder.observer.key.path, true);
}
$(holder.container).addClass('focused');
if (holder.lastObj) {
holder.lastObj.validate();
if (holder.observer && holder.observer.target) {
holder.observer.target.validate();
}
},
validated: function(isValid, model, errors) {
var errorList = errors[holder.lastId];
if (errorList && holder.lastObj.seen(holder.lastId)) {
var errorList = errors[holder.observer.key.path];
if (errorList && holder.observer.target && holder.observer.target.seen(holder.observer.key.path)) {
$(el).tooltip({title: errorList, trigger: 'focus'});
$(el).tooltip('show');
$(el).parent().addClass('has-error');
......@@ -85,26 +48,21 @@ define(['rivets', 'bootstrap'], function(rivets) {
var holder = this.validationHolder;
$(this.validationHolder.marker).after(el).remove();
$(el).off('focus', holder.focus).off('blur', holder.blur);
diveIntoObject(this.observer.target, this.observer.key.path, function(obj) {
if (obj) {
obj.off('validated', holder.validated);
if (holder.observer.target) {
holder.observer.target.off('validated', holder.validated);
}
});
delete this.validationHolder;
rivetsBinderCall(this, this.args[0], 'unbind', arguments);
},
routine: function() {
var holder = this.validationHolder;
if (holder.lastObj) {
holder.lastObj.off('validated', holder.validated);
if (holder.observer) {
holder.observer.target.off('validated', holder.validated);
}
diveIntoObject(this.observer.target, this.observer.key.path, function(obj, id) {
holder.lastObj = obj;
holder.lastId = id;
if (obj) {
obj.on('validated', holder.validated);
holder.observer = this.observer;
if (this.observer.target) {
this.observer.target.on('validated', holder.validated);
}
});
rivetsBinderCall(this, this.args[0], 'routine', arguments);
}
};
......