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) { ...@@ -45,10 +45,13 @@ define(function(require) {
45 var oldParent; 45 var oldParent;
46 var parentObserver; 46 var parentObserver;
47 var keyPath = observer.key.path; 47 var keyPath = observer.key.path;
48 var parentPath = _.collect(observer.tokens, function(token) {
49 return token.i + token.path;
50 }).join('');
48 this.on('bind', function() { 51 this.on('bind', function() {
49 render(el, 'bind'); 52 render(el, 'bind');
50 render(el, 'validated', false); 53 parentObserver = binding.observe(binding.view.models, parentPath, createTriggerProxy(this, 'parent-change'));
51 }); 54 }, this);
52 this.on('focus', function() { 55 this.on('focus', function() {
53 render(el, 'focus', false); 56 render(el, 'focus', false);
54 }); 57 });
...@@ -69,14 +72,29 @@ define(function(require) { ...@@ -69,14 +72,29 @@ define(function(require) {
69 }); 72 });
70 this.on('unbind', function() { 73 this.on('unbind', function() {
71 render(el, 'unbind'); 74 render(el, 'unbind');
75 this.trigger('stop-monitoring-parent');
76 parentObserver.unobserve();
77 parentObserver = null;
72 $(el).off('.rivets-error-binder'); 78 $(el).off('.rivets-error-binder');
73 }); 79 });
80 this.on('unbind parent-change', function() {
81 if (oldParent) {
82 this.stopListening(oldParent);
83 }
84 oldParent = null;
85 }, this);
86 this.on('bind parent-change', function() {
87 var newParent = parentObserver.value();
88 if (newParent) {
89 seen = {};
90 render(el, 'validated', false);
91 this.listenTo(newParent, 'validated', createTriggerProxy(this, 'validated'));
92 }
93 oldParent = newParent;
94 }, this);
74 var $el = $(el); 95 var $el = $(el);
75 $el.on('focus.rivets-error-binder', createTriggerProxy(this, 'focus')); 96 $el.on('focus.rivets-error-binder', createTriggerProxy(this, 'focus'));
76 $el.on('blur.rivets-error-binder', createTriggerProxy(this, 'blur')); 97 $el.on('blur.rivets-error-binder', createTriggerProxy(this, 'blur'));
77 if (observer.target) {
78 this.listenTo(observer.target, 'validated', createTriggerProxy(this, 'validated'));
79 }
80 } 98 }
81 _.extend(ErrorHandler.prototype, Backbone.Events); 99 _.extend(ErrorHandler.prototype, Backbone.Events);
82 100
......
...@@ -23,6 +23,15 @@ define(function(require) { ...@@ -23,6 +23,15 @@ define(function(require) {
23 var test; 23 var test;
24 var view; 24 var view;
25 var render; 25 var render;
26 function createTestModel(constantValue) {
27 var test = new Model({
28 constant: constantValue
29 });
30 test.validation = {
31 constant: {required: true}
32 };
33 return test;
34 };
26 beforeEach(function() { 35 beforeEach(function() {
27 render = function(el, cmd, errorList) { 36 render = function(el, cmd, errorList) {
28 render.lastCmd = cmd; 37 render.lastCmd = cmd;
...@@ -50,13 +59,8 @@ define(function(require) { ...@@ -50,13 +59,8 @@ define(function(require) {
50 Collection = Backbone.Collection.extend({model: Model}); 59 Collection = Backbone.Collection.extend({model: Model});
51 60
52 scope = new Model({ 61 scope = new Model({
53 test: test = new Model({ 62 test: test = createTestModel('CONSTANT')
54 constant: 'CONSTANT'
55 })
56 }); 63 });
57 test.validation = {
58 constant: {required: true}
59 };
60 }); 64 });
61 afterEach(function() { 65 afterEach(function() {
62 view.unbind(); 66 view.unbind();
...@@ -99,6 +103,8 @@ define(function(require) { ...@@ -99,6 +103,8 @@ define(function(require) {
99 expect(render.counts).toEqual({focus : 0, blur : 0, validatedError : 0, validatedClean : 0}); 103 expect(render.counts).toEqual({focus : 0, blur : 0, validatedError : 0, validatedClean : 0});
100 jasmine.Clock.tick(1); 104 jasmine.Clock.tick(1);
101 expect(render.lastErrorList).toBeUndefined(); 105 expect(render.lastErrorList).toBeUndefined();
106 scope.set('test', null);
107 scope.set('test', test = createTestModel(test.get('constant')));
102 }); 108 });
103 it('existing-with-render', function() { 109 it('existing-with-render', function() {
104 /* global requirejs */ 110 /* global requirejs */
......