Skip to content
Browse files

Integrating Delegator w/ Behavior for DOM updates.

  • Loading branch information...
1 parent 9bf2f51 commit 8a5d7bff1e37e366a777b85cccb94fef44f973bb @anutron committed Jun 9, 2011
Showing with 49 additions and 2 deletions.
  1. +34 −0 Source/Delegator.js
  2. +15 −2 Specs/Behavior/Delegator.Specs.js
View
34 Source/Delegator.js
@@ -16,6 +16,7 @@ provides: [Delegator]
options: {
// breakOnErrors: false,
+ getBehavior: function(){},
onError: Behavior.getLog('error'),
onWarn: Behavior.getLog('warn')
},
@@ -47,6 +48,39 @@ provides: [Delegator]
this.fireEvent('warn', arguments);
}.bind(this)
});
+
+ this.bindToBehavior(this.options.getBehavior());
+ },
+
+ bindToBehavior: function(behavior){
+ if (!behavior) return;
+ this.unbindFromBehavior();
+ this._behavior = behavior;
+ if (!this._behaviorEvents){
+ var self = this;
+ this._behaviorEvents = {
+ destroyDom: function(elements){
+ Array.from(elements).each(function(element){
+ self._behavior.cleanup(element);
+ });
+ },
+ ammendDom: function(container){
+ self._behavior.apply(container);
+ }
+ };
+ }
+ this.addEvents(this._behaviorEvents);
+ },
+
+ getBehavior: function(){
+ return this._behavior;
+ },
+
+ unbindFromBehavior: function(){
+ if (this._behaviorEvents && this._behavior){
+ this._behavior.removeEvents(this._behaviorEvents);
+ delete this._behavior;
+ }
},
attach: function(target, _method){
View
17 Specs/Behavior/Delegator.Specs.js
@@ -2,7 +2,7 @@
---
name: Delegator.Specs
description: n/a
-requires: [Behavior/Delegator, Core/DomReady]
+requires: [Behavior/Delegator, Behavior/Behavior, Core/DomReady]
provides: [Delegator.Specs]
...
*/
@@ -87,7 +87,20 @@ provides: [Delegator.Specs]
}, true);
expect(instance._getTrigger('Test3').handler).toBe(test4);
});
-
+
+ it('should bind to a behavior instance', function(){
+ var b = new Behavior();
+ var d = new Delegator({
+ getBehavior: function(){ return b; }
+ });
+ expect(d.getBehavior()).toBe(b);
+ var b2 = new Behavior();
+ d.bindToBehavior(b2);
+ expect(d.getBehavior()).toBe(b2);
+ d.unbindFromBehavior(b2);
+ expect(d.getBehavior()).toBeFalsy();
+ });
+
// Only run this spec in browsers other than IE6-8 because they can't properly simulate bubbling events
if (window.addEventListener){

0 comments on commit 8a5d7bf

Please sign in to comment.
Something went wrong with that request. Please try again.