66// Behaviors allow you to blackbox View specific interactions
77// into portable logical chunks, keeping your views simple and your code DRY.
88
9- import _ from 'underscore' ;
9+ import { extend as _extend , uniqueId , result } from 'underscore' ;
1010import extend from './utils/extend' ;
11- import getNamespacedEventName from './utils/get-namespaced-event-name' ;
1211import CommonMixin from './mixins/common' ;
1312import DelegateEntityEventsMixin from './mixins/delegate-entity-events' ;
14- import TriggersMixin from './mixins/triggers' ;
1513import UIMixin from './mixins/ui' ;
14+ import ViewEventsMixin from './mixins/view-events' ;
1615
1716const ClassOptions = [
1817 'collectionEvents' ,
@@ -29,8 +28,12 @@ const Behavior = function(options, view) {
2928 // to the view.
3029 this . view = view ;
3130
31+
3232 this . _setOptions ( options , ClassOptions ) ;
33- this . cid = _ . uniqueId ( this . cidPrefix ) ;
33+ this . cid = uniqueId ( this . cidPrefix ) ;
34+
35+ this . _initViewEvents ( ) ;
36+ this . setElement ( ) ;
3437
3538 // Construct an internal UI hash using the behaviors UI
3639 // hash combined and overridden by the view UI hash.
@@ -39,7 +42,7 @@ const Behavior = function(options, view) {
3942 // This order will help the reuse and share of a behavior
4043 // between multiple views, while letting a view override
4144 // a selector under an UI key.
42- this . ui = _ . extend ( { } , _ . result ( this , 'ui' ) , _ . result ( view , 'ui' ) ) ;
45+ this . ui = _extend ( { } , result ( this , 'ui' ) , result ( view , 'ui' ) ) ;
4346
4447 // Proxy view triggers
4548 this . listenTo ( view , 'all' , this . triggerMethod ) ;
@@ -52,12 +55,9 @@ Behavior.extend = extend;
5255// Behavior Methods
5356// --------------
5457
55- _ . extend ( Behavior . prototype , CommonMixin , DelegateEntityEventsMixin , TriggersMixin , UIMixin , {
58+ _extend ( Behavior . prototype , CommonMixin , DelegateEntityEventsMixin , UIMixin , ViewEventsMixin , {
5659 cidPrefix : 'mnb' ,
5760
58- // This is a noop method intended to be overridden
59- initialize ( ) { } ,
60-
6161 // proxy behavior $ method to the view
6262 // this is useful for doing jquery DOM lookups
6363 // scoped to behaviors view.
@@ -67,6 +67,8 @@ _.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMix
6767
6868 // Stops the behavior from listening to events.
6969 destroy ( ) {
70+ this . _undelegateViewEvents ( ) ;
71+
7072 this . stopListening ( ) ;
7173
7274 this . view . _removeBehavior ( this ) ;
@@ -76,10 +78,13 @@ _.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMix
7678 return this ;
7779 } ,
7880
79- proxyViewProperties ( ) {
80- this . $el = this . view . $el ;
81+ setElement ( ) {
82+ this . _undelegateViewEvents ( ) ;
83+
8184 this . el = this . view . el ;
8285
86+ this . _delegateViewEvents ( this . view ) ;
87+
8388 return this ;
8489 } ,
8590
@@ -110,36 +115,6 @@ _.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMix
110115 this . _undelegateEntityEvents ( this . view . model , this . view . collection ) ;
111116
112117 return this ;
113- } ,
114-
115- _getEvents ( ) {
116- if ( ! this . events ) { return ; }
117-
118- // Normalize behavior events hash to allow
119- // a user to use the @ui . syntax.
120- const behaviorEvents = this . normalizeUIKeys ( _ . result ( this , 'events' ) ) ;
121-
122- // binds the handler to the behavior and builds a unique eventName
123- return _ . reduce ( behaviorEvents , ( events , behaviorHandler , key ) => {
124- if ( ! _ . isFunction ( behaviorHandler ) ) {
125- behaviorHandler = this [ behaviorHandler ] ;
126- }
127- if ( ! behaviorHandler ) { return events ; }
128- key = getNamespacedEventName ( key , this . cid ) ;
129- events [ key ] = behaviorHandler . bind ( this ) ;
130- return events ;
131- } , { } ) ;
132- } ,
133-
134- // Internal method to build all trigger handlers for a given behavior
135- _getTriggers ( ) {
136- if ( ! this . triggers ) { return ; }
137-
138- // Normalize behavior triggers hash to allow
139- // a user to use the @ui . syntax.
140- const behaviorTriggers = this . normalizeUIKeys ( _ . result ( this , 'triggers' ) ) ;
141-
142- return this . _getViewTriggers ( this . view , behaviorTriggers ) ;
143118 }
144119} ) ;
145120
0 commit comments