516dd9f1 by Adam Heath

Create an observer to detect when the instance of the 'parent' changes;

there are no real test cases yet.
1 parent 27ac8600
......@@ -45,10 +45,13 @@ define(function(require) {
var oldParent;
var parentObserver;
var keyPath = observer.key.path;
var parentPath = _.collect(observer.tokens, function(token) {
return token.i + token.path;
}).join('');
this.on('bind', function() {
render(el, 'bind');
render(el, 'validated', false);
});
parentObserver = binding.observe(binding.view.models, parentPath, createTriggerProxy(this, 'parent-change'));
}, this);
this.on('focus', function() {
render(el, 'focus', false);
});
......@@ -69,14 +72,29 @@ define(function(require) {
});
this.on('unbind', function() {
render(el, 'unbind');
this.trigger('stop-monitoring-parent');
parentObserver.unobserve();
parentObserver = null;
$(el).off('.rivets-error-binder');
});
this.on('unbind parent-change', function() {
if (oldParent) {
this.stopListening(oldParent);
}
oldParent = null;
}, this);
this.on('bind parent-change', function() {
var newParent = parentObserver.value();
if (newParent) {
seen = {};
render(el, 'validated', false);
this.listenTo(newParent, 'validated', createTriggerProxy(this, 'validated'));
}
oldParent = newParent;
}, this);
var $el = $(el);
$el.on('focus.rivets-error-binder', createTriggerProxy(this, 'focus'));
$el.on('blur.rivets-error-binder', createTriggerProxy(this, 'blur'));
if (observer.target) {
this.listenTo(observer.target, 'validated', createTriggerProxy(this, 'validated'));
}
}
_.extend(ErrorHandler.prototype, Backbone.Events);
......
......@@ -23,6 +23,15 @@ define(function(require) {
var test;
var view;
var render;
function createTestModel(constantValue) {
var test = new Model({
constant: constantValue
});
test.validation = {
constant: {required: true}
};
return test;
};
beforeEach(function() {
render = function(el, cmd, errorList) {
render.lastCmd = cmd;
......@@ -50,13 +59,8 @@ define(function(require) {
Collection = Backbone.Collection.extend({model: Model});
scope = new Model({
test: test = new Model({
constant: 'CONSTANT'
})
test: test = createTestModel('CONSTANT')
});
test.validation = {
constant: {required: true}
};
});
afterEach(function() {
view.unbind();
......@@ -99,6 +103,8 @@ define(function(require) {
expect(render.counts).toEqual({focus : 0, blur : 0, validatedError : 0, validatedClean : 0});
jasmine.Clock.tick(1);
expect(render.lastErrorList).toBeUndefined();
scope.set('test', null);
scope.set('test', test = createTestModel(test.get('constant')));
});
it('existing-with-render', function() {
/* global requirejs */
......